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

version 5, allowUnknownOption(true) not work #1278

Closed
zhouhoujun opened this issue Jun 10, 2020 · 7 comments
Closed

version 5, allowUnknownOption(true) not work #1278

zhouhoujun opened this issue Jun 10, 2020 · 7 comments

Comments

@zhouhoujun
Copy link

version 5, allowUnknownOption(true) not work.
eg.

program
    .command('build [taskfile]')
    .description('build the application')
    .option('--config [string]', 'path to configuration file for activities build')
    .option('--debug [bool]', 'enable debug log or not')
    .option('-d, --deploy [bool]', 'run deploy activity')
    .allowUnknownOption(true)
    .action((taskfile, options) => {
        requireRegisters();
        taskfile = vaildifyFile(taskfile);
        if (options.boot) {
            requireCwd(taskfile);
        } else {
            runActivity(taskfile, options);
        }
    });
@shadowspawn
Copy link
Collaborator

The behaviour did change in v5, but working in my test.

What did you type on command line, and what is not working?

const { program } = require('commander');

program
    .command('build [taskfile]')
    .description('build the application')
    .option('--config [string]', 'path to configuration file for activities build')
    .option('--debug [bool]', 'enable debug log or not')
    .option('-d, --deploy [bool]', 'run deploy activity')
    .allowUnknownOption(true)
    .action((taskfile, cmd) => {
      console.log('args:', cmd.args);
      console.log('opts:', cmd.opts());
    });
  
program.parse();
$ node index.js build --config aaa --unexpected
args: [ '--unexpected' ]
opts: { config: 'aaa', debug: undefined, deploy: undefined }

@mshima

This comment has been minimized.

@shadowspawn

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 10, 2020

@zhouhoujun

I just noticed your example code checks options.boot. Did you expect Commander to process the unknown options into properties? Commander does not do that, it just allows the unknown options to be included on the command line for further processing by your code or the commands the arguments (including unknown options) are passed to.

@zhouhoujun
Copy link
Author

@shadowspawn
eg.

xxx build  --deploy --setversion=1.1.0

will get --setversion=1.1.0 as taskfile

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 11, 2020

Yes. The allowUnknownOptions just suppresses the error message for unknown options and passes them through as arguments for further processing by you.

const { program } = require('commander');

program
    .command('build <taskfile>')
    .description('build the application')
    .option('--config <string>', 'path to configuration file for activities build')
    .option('--debug', 'enable debug log or not')
    .option('-d, --deploy', 'run deploy activity')
    .allowUnknownOption(true)
    .action((taskfile, cmd) => {
      console.log('taskfile: ', taskfile);
      console.log('args: ', cmd.args);
      console.log('opts: ', cmd.opts());
    });
  
program.parse();
$ node index.js build  --deploy NAME --setversion=1.1.0
taskfile:  NAME
args:  [ 'NAME', '--setversion=1.1.0' ]
opts:  { config: undefined, debug: undefined, deploy: true }

A couple of other optional suggestions for your example program:

  1. You probably do not want the [bool].
  2. I recommend <required> values for options over [optional] values, unless you really want optional.

@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

3 participants