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

Unexpected behavior when options passed before command, but fine if passed after #895

Closed
philsturgeon opened this issue Dec 7, 2018 · 2 comments

Comments

@philsturgeon
Copy link

philsturgeon commented Dec 7, 2018

I maintain Speccy and we've got a confusing bug.

Our commander.js usage is all here and as far as I can tell im following documented usage. But, our options are not working as expected.

All commands suffer the same issue, butt an example:

program
    .command('lint <file-or-url>')
    .description('ensure specs are not just valid OpenAPI, but lint against specified rules')
    .option('-q, --quiet', 'reduce verbosity')
    .option('-r, --rules [ruleFile]', 'provide multiple rules files', collect, [])
    .option('-s, --skip [ruleName]', 'provide multiple rules to skip', collect, [])
    .option('-j, --json-schema', 'treat $ref like JSON Schema and convert to OpenAPI Schema Objects (default: false)')
    .option('-v, --verbose', 'increase verbosity')
.action(lint.command);

I would expect the command speccy lint -j foo.yaml to work the same way that I would expect speccy lint foo.yaml -j to work, but the first command gives this output:

 node speccy lint --json-schema test/samples/json-schema/openapi.yaml
Usage: speccy <command> [options]

Options:
  -V, --version                    output the version number
  -c, --config [configFile]        config file (containing JSON/YAML). See README for potential values.
  -h, --help                       output usage information

Commands:
  lint [options] <file-or-url>     Ensure specs are not just valid OpenAPI, but lint against quality control rules
  resolve [options] <file-or-url>  Pull in external $ref files to create one mega-file
  serve [options] <file-or-url>    Display specifications in beautiful human readable documentation

Any ideas here?

@shadowspawn
Copy link
Collaborator

You are getting a false positive from this line in speccy.js:

if (!program.args.length) program.help();

program.args has what is left-over from commander parsing, whereas for displaying help you want to check the original arguments passed to your program. (I am a bit surprised by the difference in behaviour you are seeing, but not relevant to fix.)

The Output Help example code is:

if (!process.argv.slice(2).length) {
  ...
}

@philsturgeon
Copy link
Author

Thank you, that was it!

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