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

Subcommand gets parsed as option #828

Closed
martinpaljak opened this issue Oct 2, 2019 · 3 comments
Closed

Subcommand gets parsed as option #828

martinpaljak opened this issue Oct 2, 2019 · 3 comments
Labels
theme: parser An issue or change related to the parser type: bug 🐛 type: enhancement ✨
Milestone

Comments

@martinpaljak
Copy link

I'm building something that will execute commands dynamically, basically an app launcher that resolves app names during runtime. The command itself also has a set of options.

I have

@Option(names = {"-r", "--reader"}, arity = "0..1", description = "Use reader", paramLabel = "<reader>", fallbackValue = "")
    String reader;
 @Parameters
    String[] params = {};

and

 @Command(name = "run", description = "Run specified application.")
    public int runApp(@Parameters(paramLabel = "<app>", index = "0") String app, @Parameters(index = "1..*") String[] args) {

Also, I'd like the "run" subcommand be the default, so when I type "mycmd app arg1 arg2" the above would be triggered from the generic call():

// Default is to run apps
        if (params.length > 0 && weHaveApp(params[0])) {
            return runApp(params[0], Arrays.copyOfRange(params, 1, params.length));
        }

I'd like to use "mycmd -r run app arg1 arg2 --something" to run app with arg1 and arg2 and --something as well as with the fallback value assigned to reader (the logic behind it is "if --reader specified, use it, otherwise trigger UI related to reader selection") Right now "run" is assigned to the reader.

Is something like this possible?

@remkop remkop added this to the 4.0.5 milestone Oct 2, 2019
remkop added a commit that referenced this issue Oct 2, 2019
…n values for options with optional parameters.
@remkop
Copy link
Owner

remkop commented Oct 2, 2019

Thanks for raising this.
Picocli has logic to prevent other options from being assigned to the --reader option, but as of 4.0.4, it does not check for subcommands. I agree that it should check for subcommands as well.

I've pushed a fix for this to master. Can you verify?

@remkop
Copy link
Owner

remkop commented Oct 2, 2019

I’m still looking at further improvements so that end users can use quoted arguments to prevent the value being interpreted as a subcommand. For example:

# invokes the run subcommand
mycmd -r run app arg1 arg2 --something

# “run” is assigned to reader option 
mycmd -r “run” app arg1 arg2 --something

The above works when parser setting trimQuotes == false (the default), but currently not when trimQuotes == true, because the quotes are trimmed before the value is interpreted. I am thinking about changing this, but still considering the impact...

@remkop
Copy link
Owner

remkop commented Oct 8, 2019

I'm going to leave the quoting logic alone for now.
The reported issue is fixed on master, we can look at further improving options with optional values later.

@remkop remkop closed this as completed Oct 8, 2019
@remkop remkop reopened this Nov 17, 2019
remkop added a commit that referenced this issue Nov 17, 2019
@remkop remkop closed this as completed Nov 19, 2019
@remkop remkop added the theme: parser An issue or change related to the parser label Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: parser An issue or change related to the parser type: bug 🐛 type: enhancement ✨
Projects
None yet
Development

No branches or pull requests

2 participants