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

Disable required checks from subcommand #268

Closed
ymenager opened this issue Jan 16, 2018 · 9 comments
Closed

Disable required checks from subcommand #268

ymenager opened this issue Jan 16, 2018 · 9 comments

Comments

@ymenager
Copy link

I wanted to have the ability to have a subcommand which will disable validation of required parameters in a parent subcommand.

I started working on it by following the same model as isHelpRequested().

However unless I'm completely missing something it doesn't seem like that's going to work since help doesn't seem to disable required in parent subcommands :(

So it kinda looks like this would end up requiring major architectural changes so that the required checks are done at the end after all parsing is completed, is that correct ?

@remkop
Copy link
Owner

remkop commented Jan 19, 2018

Very good point! This is also useful for the auto-help feature included in picocli 3.0-alpha-1 (in master, not released yet).

Quick recap: at the moment there are two places where required parameters are checked:

  1. during parsing, when a subcommand is detected, before the remaining command line args are parsed by the subcommand's interpreter
  2. after the command line has been fully parsed

In both cases, the required params check is omitted if a help option was previously encountered.

One idea is to add a new attribute to @Command, something like isHelpCommand, and the required params check would be omitted if either a help option or a help command was encountered.

Now that I'm thinking about it, perhaps the autoHelp attribute should be renamed to installAutoHelp or something else to avoid confusion with the isHelpCommand attribute.

Thoughts?

@ymenager
Copy link
Author

Would it cause any problems to remove 1), and allow the check in 2) to be disabled ?

@remkop
Copy link
Owner

remkop commented Jan 19, 2018

Can you explain more details about your use case? I want to understand the problem better before jumping into possible solutions. Why does your subcommand need to disable validation of required parameters in its parent command?

@ymenager
Copy link
Author

basically I have a subcommand with many subcommands

all of the subcommands of the subcommand require a username and password, which I've added to the parent subcommand.

However one of those sub-sub-commands doesn't require username and password, so I would have preferred to be able to just disable the required check on the parent, rather than having to move username and password into every child subcommand (so the use case does has a workaround, but i thought it would be nicer to have that flexibility in picocli)

@remkop
Copy link
Owner

remkop commented Jan 19, 2018

That sounds like the model doesn't quite fit. Should the parent subcommand really require a username and password if not all its subcommands need it? An alternative way to model this would be to create a username/password mixin that is only added to the subcommands that actually need it.

@remkop
Copy link
Owner

remkop commented Jan 19, 2018

Yet another alternative is to do the validation in your app instead of letting picocli do it.

To accomplish this, make username and password non-required options of the parent command. Add a method to the parent command that performs the validation (prints the "missing required parameter..." message and usage help if username or password is missing). Call this method only from the subcommands that actually require the username and password.

@ymenager
Copy link
Author

I could move it to the subcommand using mixin or subclassing, in my specific use case it's workaroundable... i was just thinking having that flexibility could be useful for other uses cases as well (an help subcommand like you mentioned would be another one)

@remkop
Copy link
Owner

remkop commented Jan 19, 2018

I am not 100% happy with the current implementation for AutoHelp. I would like users to be able to create custom help commands.

At least two things are missing for that to work:

  • switching off required validation (one idea is to introduce an isHelpCommand attribute for this)
  • the custom help command needs to be able to access the CommandSpec or CommandLine of its parent command so it can find its "siblings" - the other subcommands of the parent command

I'm glad to have this discussion with you since it is not easy to spot what is missing when trying to think things through by yourself. Appreciated!

@remkop
Copy link
Owner

remkop commented Feb 3, 2018

Commands can now be annotated with @Command(isHelpCommand = true).
If the parser encounters a subcommand on the command line that has the isHelpCommand attribute, then it will not validate the required options and positional parameters of the parent command.

This is now in master.
Can you verify this meets your needs?

remkop added a commit that referenced this issue Feb 4, 2018
remkop added a commit that referenced this issue Feb 4, 2018
remkop added a commit that referenced this issue Feb 7, 2018
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