Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When using "help <SubCommand>", the exit code is 0, despite exitCodeOnUsageHelp = ExitCode.USAGE #2355

Closed
marco-brandizi opened this issue Dec 9, 2024 · 3 comments
Milestone

Comments

@marco-brandizi
Copy link

marco-brandizi commented Dec 9, 2024

The title says almost all: I have a CLI app that is arranged as main command + subcommands. When I issue something like: java MainCommand help subCommand, the OS exit code is 0, not 2, despite both MainCommand.class and SubCommand are annotated with:

@Command ( ... exitCodeOnUsageHelp = ExitCode.USAGE, exitCodeOnVersionHelp = ExitCode.USAGE)

when doing java MainCommand -h, it exits with 2 as expected. Unless I'm missing something, to me it is a bug.

Until this is changed, it's possible to write a workaround like: in the static main(), check if the first argument is 'help'.

@remkop
Copy link
Owner

remkop commented Dec 9, 2024

I think you are correct, and this is a bug in the implementation of picocli.CommandLine.HelpCommand.

This class currently implements Runnable.
To fix the issue, it should implement Callable<Integer> and should either
return subcommand.getCommandSpec().exitCodeOnUsageHelp() if a subcommand was specified, or
return parent.getCommandSpec().exitCodeOnUsageHelp() if not.

Will you be able to provide a pull request with tests for this?

@remkop remkop added this to the 4.7.7 milestone Dec 9, 2024
marco-brandizi added a commit to marco-brandizi/picocli that referenced this issue Dec 11, 2024
@marco-brandizi
Copy link
Author

marco-brandizi commented Dec 11, 2024

Hi @remkop, I've tried to write a fix, but unfortunately, I don't have much time and I barely managed to change HelpCommand. It's hard to write a test and even without any changes, my clone doesn't build for me, mvn clean package gives me errors (starting with package org.junit does not exist), I can't use Gradle.

@remkop
Copy link
Owner

remkop commented Dec 20, 2024

Note to self:

// src/test/java/picocli/HelpSubCommandTest.java

    @Command(name = "issue2355", exitCodeOnUsageHelp = 123)
    static class Issue2355 implements Runnable {
        @Override public void run() {}
    }

    @Test
    public void testIssue2355ExitCode() {
        @Command(name = "top", subcommands = Issue2355.class) class Top { }
        int actual = new CommandLine(new Top()).execute("help", "issue2355");
        assertEquals(123, actual);
    }

@remkop remkop closed this as completed in ec52e70 Jan 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants