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

sub-command not executed if using --version option #797

Closed
ScandalousMan opened this issue May 9, 2018 · 5 comments
Closed

sub-command not executed if using --version option #797

ScandalousMan opened this issue May 9, 2018 · 5 comments

Comments

@ScandalousMan
Copy link

Hello,

command :

> spm module publish --version 1.0.0

expected result:
action requested should be executed passing the value 1.0.0 to option.version for sub-command module

actual result:
program displays current CLI version and exits

Here are the files in question :

// file: spm
#!/usr/bin/env node
let Program = require('commander')

Program
.description('CLI for spm')
.version('1.0.0')
.command('module', 'for actions about modules').alias('m')
.parse(process.argv)

if (Program.args.length === 0) {
  Program.help()
}
// EOF

// file: spm-module
#!/usr/bin/env node
let Program = require('commander')

Program
    .command('publish')
    .alias('p')
    .description(`to send your module to spm registry and factory`)
    .arguments('[moduleName]')
    .option('-v, --version <version>', `to specify the module's version`)
    .action((moduleName, options) => {
      console.log('option version works:', options.version)
   })
.parse(process.argv)

if (Program.args.length === 0) {
  Program.help()
}
// EOF

I have found many open/closed issues about options in sub-commands, and others for the version option, but I cannot manage to fix this behaviour. Is it a WAI or is there a way to achieve what I'm trying to do ? Thanks a lot in advance !

@shadowspawn
Copy link
Collaborator

The .version() is implicitly adding options for -V, --version and triggers before your command fires, which I think is working as intended. To resolve the issue one of the two uses of --version needs to change and it can be the global one:
https://github.com/tj/commander.js#version-option

@ScandalousMan
Copy link
Author

Hello John, thanks for the reply. I was trying to find a way to make a difference depending on the option position in the command line, e.g.

> spm --version module

and

> spm module --version

Do you confirme this is not a feature handled by commander ? Is there a reason for this or would you be interested by a PR ?

Thanks and have a good day !

@shadowspawn
Copy link
Collaborator

Disclaimer: I am a user not a maintainer. (So comments are IMHO.)

I do not think this feature is handled by commander. I think positional would match what people expect in at least some situations, but treating options globally may be by design.

It is particularly surprising for --version which makes little sense when used with other arguments and options! But it feels quite natural with --help:

spm --help publish
spm publish --help

For an external reference, the first program I noticed non-positional global options with was Mercurial:

hg --repo custom status
hg status --repo custom

@shadowspawn shadowspawn added the docs README (or other docs) could be improved label Mar 20, 2019
@shadowspawn shadowspawn removed the docs README (or other docs) could be improved label May 27, 2019
@shadowspawn
Copy link
Collaborator

(I am a maintainer now!)

This issue has not had any activity in over six months. It isn't likely to get acted on due to this report.

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

Thank you for your contributions.

@shadowspawn
Copy link
Collaborator

Pull Request opened to add .enablePositionalOptions() and .passThroughOptions(): #1427

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