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

conditional option parsing #1555

Closed
proohit opened this issue Jun 30, 2021 · 2 comments
Closed

conditional option parsing #1555

proohit opened this issue Jun 30, 2021 · 2 comments

Comments

@proohit
Copy link

proohit commented Jun 30, 2021

Is there any way to (re-)parse options based on conditions? I'm thinking of something like

program.option('--interactive')
program.parse();
let opts = program.opts()
if(opts.interactive) {
    // options are optional here
} else {
    // if condition is false, fallback to required options instead
    program.requiredOption('--requiredOption')
    program.parse();
    opts = program.opts()
    // use conditional opts
}

Currently, when trying to run this, I'm getting error: unknown option '--requiredOption' because the option isn't added at the time of the first parsing.

EDIT: One workaround may be to declare all options (even the conditionally required) as optional ahead of time and 'validate' them when needed conditionally with exiting with .help() if they are missing.

@shadowspawn
Copy link
Collaborator

There isn't a way to reparse as such. I recommend you create a new Command for each parse to avoid left over state.

Parsing twice, in the first parse you might set .allowUnknownOption() and leave out some options, or at least don't make them "required" as you suggested. If first parse discovers not interactive, then parse again with the full set of options (and errors for unknown).

(If there are not other options and --interative is basically alone when used, you could even check for it yourself.)

@shadowspawn
Copy link
Collaborator

An answer was provided, and no further activity in a month. Closing this as resolved.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants