-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Using .passThroughOptions()
without enabling .enablePositionalOptions()
#2281
Comments
The default parsing is that options for the root command (or any command) can come before or after subcommands. The parsing looks for global options before it looks for subcommands. So in your example, if the root command also had same It would take a rewrite of the parsing to allow the leaf command to change the parsing behaviour in isolation, which was more than I wanted to do to add the feature. |
So to confirm, you use global options after subcommands in other parts of your program? Like: program subcmd2 --programOption |
Well, kinda. I actually have a function which allows all options to be available on all levels of the program. For example, if For a more concrete example, I have defined the Now, I need to enable pass through options because I am implementing a command like Unix's Relevant code/**
* Overrides opts to return all options set in the command hierarchy
*/
public opts<T extends commander.OptionValues>(): T {
const opts = super.opts<T>();
if (this.parent != null) {
// Override the current options with parent options
// global option values are captured by the root command
return Object.assign(opts, this.parent.opts<T>());
} else {
return opts;
}
} |
Somewhat related, there are examples adding options to multiple subcommands in: https://github.com/tj/commander.js/blob/master/examples/global-options-added.js |
Yeah, this is what I am doing. For reference, here is the relevant file in my project: https://github.com/MatrixAI/Polykey-CLI/blob/staging/src/CommandPolykey.ts Reiterating here, but I need to enable pass through options for only one subcommand, so I can capture the raw arguments (including the |
No, not and keep the other behaviours you have described.
Yes. I had mentioned the example file because if you have the options at the leaf subcommands anyway, you could refactor and remove the redundant global copy. |
The example I provided was simplified. In actuality, a usage of the program can look like this
Now, this command currently works in all these configurations
And I have a lot of commands. It would have a lot of repetition, and it would be very easy to forget adding the option to a leaf command, and can break the program. This is why the project implements global opts, allowing access of all options on all levels basically. This is why it is such an issue. |
One approach is making
You did mention the resulting format in one of your comments, so just pointing out you could embrace the approach:
|
https://github.com/MatrixAI/Polykey-CLI/blob/staging/src/secrets/CommandEnv.ts I already do kind of have this. However, even this approach needs two of the this.argument(
'<args...>',
'command and arguments formatted as [envPaths...][-- cmd [cmdArgs...]]',
binParsers.parseEnvArgs,
); Am I doing something wrong with my implementation for this? |
Without You could use a different sequence of characters for terminating
|
This is something I don't want to do. I could have easily added an option or a flag to switch contexts as well, but I want the behaviour to remain as close to Unix's
I tried this and it didn't work. I even tried making
Is there no workaround to achieve my desired behaviour except for refactoring to enable positional arguments? |
Ok, I think I understand the constraints now. Some other parsing libraries return the arguments after the I think by looking up the
% node index.mjs subcommand path1 path2 -- cmd cmdArg1 --cmdOption1 -- cmdArg2
{
envPaths: [ 'path1', 'path2' ],
cmdName: 'cmd',
cmdArgs: [ 'cmdArg1', '--cmdOption1', '--', 'cmdArg2' ]
} |
This is perfect for our use case. Thanks! I'll be closing this issue now. |
Is there any way to allow passing options through without enabling positional options globally? Part of my program expects it to be disabled, and is built around that. However, one subcommand needs this functionality, and I cannot selectively enable positional arguments for one command.
For
subcmd
, I need to get all arguments after the first positional argument, like['arg1', 'arg2', '--opt']
, but I also have a--opt
flag for the subcommand, so the variadic arguments treat the second--opt
as part of the subcommand command, which is not what should happen.Trying to enable
passThroughOptions
does not achieve anything as positional arguments are disabled globally. Why do we need to enable positional arguments globally to usepassThroughOptions
in a subcommand anyways?The text was updated successfully, but these errors were encountered: