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

opts() does not display all options #1610

Closed
kuncevic opened this issue Sep 23, 2021 · 6 comments
Closed

opts() does not display all options #1610

kuncevic opened this issue Sep 23, 2021 · 6 comments

Comments

@kuncevic
Copy link

kuncevic commented Sep 23, 2021

I have this code:

program
  .command('pm [name]')
  .addOption(new Option('-g').hideHelp())
  .description('...')
  .action((name, options) => {
    console.log(program.opts()); // does not list -g if -g specified as a param
    console.log(options.g) // returns true if -g specified
  });

I am using typescript and calling the program through the npm start script:

"start": "node -r ts-node/register ./src/bin/ngvm.ts",

npm start -- pm -g

here is the console.log(program):

output
 Command {
  _events: [Object: null prototype] {
    'option:version': [Function (anonymous)],
    'option:trace': [Function (anonymous)],
    'option:debug': [Function (anonymous)],
    afterHelp: [Function (anonymous)]
  },
  _eventsCount: 4,
  _maxListeners: undefined,
  commands: [
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'x',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'xyz',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'global',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'local',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'remote',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'node',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'system',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [Array],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'release',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      commands: [],
      options: [Array],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [Array],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'install',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [Array],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      commands: [],
      options: [],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [],
      args: [],
      rawArgs: [],
      processedArgs: [],
      _scriptPath: null,
      _name: 'uninstall',
      _optionValues: {},
      _optionValueSources: {},
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [Array],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    },
    Command {
      _events: [Object: null prototype],
      _eventsCount: 1,
      _maxListeners: undefined,
      commands: [],
      options: [Array],
      parent: [Circular *1],
      _allowUnknownOption: false,
      _allowExcessArguments: true,
      _args: [Array],
      args: [],
      rawArgs: [],
      processedArgs: [Array],
      _scriptPath: null,
      _name: 'pkgmanager',
      _optionValues: [Object],
      _optionValueSources: [Object],
      _storeOptionsAsProperties: false,
      _actionHandler: [Function: listener],
      _executableHandler: false,
      _executableFile: null,
      _defaultCommandName: null,
      _exitCallback: null,
      _aliases: [Array],
      _combineFlagAndOptionalValue: true,
      _description: 'Display Angular CLI versions that are installed on your system',
      _argsDescription: undefined,
      _enablePositionalOptions: false,
      _passThroughOptions: false,
      _lifeCycleHooks: {},
      _showHelpAfterError: false,
      _showSuggestionAfterError: false,
      _outputConfiguration: [Object],
      _hidden: false,
      _hasHelpOption: true,
      _helpFlags: '-h, --help',
      _helpDescription: 'display help for command',
      _helpShortFlag: '-h',
      _helpLongFlag: '--help',
      _addImplicitHelpCommand: undefined,
      _helpCommandName: 'help',
      _helpCommandnameAndArgs: 'help [command]',
      _helpCommandDescription: 'display help for command',
      _helpConfiguration: {},
      [Symbol(kCapture)]: false
    }
  ],
  options: [
    Option {
      flags: '-V, --version',
      description: 'output the version number',
      required: false,
      optional: false,
      variadic: false,
      mandatory: false,
      short: '-V',
      long: '--version',
      negate: false,
      defaultValue: undefined,
      defaultValueDescription: undefined,
      envVar: undefined,
      parseArg: undefined,
      hidden: false,
      argChoices: undefined
    },
    Option {
      flags: '-t, --trace',
      description: 'display trace information for commands',
      required: false,
      optional: false,
      variadic: false,
      mandatory: false,
      short: '-t',
      long: '--trace',
      negate: false,
      defaultValue: undefined,
      defaultValueDescription: undefined,
      envVar: undefined,
      parseArg: undefined,
      hidden: true,
      argChoices: undefined
    },
    Option {
      flags: '-d, --debug',
      description: 'display debug information for commands',
      required: false,
      optional: false,
      variadic: false,
      mandatory: false,
      short: '-d',
      long: '--debug',
      negate: false,
      defaultValue: undefined,
      defaultValueDescription: undefined,
      envVar: undefined,
      parseArg: undefined,
      hidden: true,
      argChoices: undefined
    }
  ],
  parent: null,
  _allowUnknownOption: true,
  _allowExcessArguments: true,
  _args: [],
  args: [ 'pm', '-g' ],
  rawArgs: [
    '/Users/A/Work/ngvm/node_modules/.bin/ts-node',
    '/Users/A/Work/ngvm/src/bin/ngvm.ts',
    'pm',
    '-g'
  ],
  processedArgs: [],
  _scriptPath: '/Users/A/Work/ngvm/src/bin/ngvm.ts',
  _name: 'ngvm',
  _optionValues: {},
  _optionValueSources: {},
  _storeOptionsAsProperties: false,
  _actionHandler: null,
  _executableHandler: false,
  _executableFile: null,
  _defaultCommandName: null,
  _exitCallback: null,
  _aliases: [],
  _combineFlagAndOptionalValue: true,
  _description: '',
  _argsDescription: undefined,
  _enablePositionalOptions: false,
  _passThroughOptions: false,
  _lifeCycleHooks: {
    preAction: [ [Function (anonymous)] ],
    postAction: [ [Function (anonymous)] ]
  },
  _showHelpAfterError: false,
  _showSuggestionAfterError: false,
  _outputConfiguration: {
    writeOut: [Function: writeOut],
    writeErr: [Function: writeErr],
    getOutHelpWidth: [Function: getOutHelpWidth],
    getErrHelpWidth: [Function: getErrHelpWidth],
    outputError: [Function: outputError]
  },
  _hidden: false,
  _hasHelpOption: true,
  _helpFlags: '-h, --help',
  _helpDescription: 'display help for command',
  _helpShortFlag: '-h',
  _helpLongFlag: '--help',
  _addImplicitHelpCommand: undefined,
  _helpCommandName: 'help',
  _helpCommandnameAndArgs: 'help [command]',
  _helpCommandDescription: 'display help for command',
  _helpConfiguration: {},
  _version: '0.0.2',
  _versionOptionName: 'version',
  [Symbol(kCapture)]: false
}
@shadowspawn
Copy link
Collaborator

shadowspawn commented Sep 23, 2021

Adding a subcommand like this returns a new command, and the -g option is on the subcommand and not on the program. This may help make it clearer:

program.option('--global');
const pm = program.command('pm [name]');
pm.option('--local')...

There is not a single call to get all of the options with the global and local options combined. There is an open issue asking what people would like along these lines, which includes a fuller example: #1551

@kuncevic
Copy link
Author

kuncevic commented Sep 23, 2021

Right, i c, the current behavior is a bit confusing when it comes to that, I believe this make a lot of sense #1551 (comment)

What is the best way to access this -g option today using program... ?

p.s I just approach it this way for now program.args.includes('-g')

@shadowspawn
Copy link
Collaborator

What is the best way to access this -g option today using program... ?

I am not sure what you are asking?

There is example code in #1551 which writes out the program options and the subcommand options.

Do you want access to the global options in your action handler without using a program variable? The action handler is also passed the subcommand and there is a .parent property which can be used to access the program from a subcommand.

@kuncevic
Copy link
Author

Oh, right, I should of mentioned that initially,I wanted to access options out of the action handler. That is what I do now:

import { program } from './bin/ngvm'; //export my program thing
const g = program.args.includes('-g');

I hope it make sense for you now.

@shadowspawn
Copy link
Collaborator

Commander does not track which subcommand was called. A couple of ideas, but may not suit your use:

  • you could store the options or the subcommand from the action handler for later reference
  • if the option is global, you could move it onto the program instead of the subcommand (and then it would be available from the program as you expected)

@kuncevic
Copy link
Author

Yeah I c, your point, the second suggestion seems valid for my case, I will play around with that.

Thanks for support!

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