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

Init value of option '--name' is a [Function] #648

Closed
bySabi opened this issue Jun 25, 2017 · 6 comments
Closed

Init value of option '--name' is a [Function] #648

bySabi opened this issue Jun 25, 2017 · 6 comments

Comments

@bySabi
Copy link

bySabi commented Jun 25, 2017

When a option --name is not set on CLI, initial value is a [Function]

Example:

var Command = require('commander').Command;
var program = new Command('my-cli');

program
  .version('0.0.1')
  .option('--name <name>', 'Set name')
  .option('--name-prefix <name>', 'Set prefix name')
 .parse(process.argv);
  })
...

console.log('name: ' , program.name)
console.log('namePrefix: ', program.namePrefix)

The ouput of $ my-cli --name-prefix=test is:

name: [Function]
namePrefix: test

Here name should be undefined

The ouput of $ my-cli --name=test is:

name: test
namePrefix: undefined

Here is ok!

IMHO a widely use option like '--name' that collide with [Command.name] should have a dedicated check and initialization branch in code.

@bySabi
Copy link
Author

bySabi commented Jun 26, 2017

I create a workaround module for this problem: safe-commander

@elliot-nelson
Copy link

There are a couple flags like this. --parent, for example.

@bySabi
Copy link
Author

bySabi commented Jun 28, 2017

@elliot-nelson you are right, too many options collide. That's for I could not wait for commander 3.x.x release and create safe-commander.js.

Any feedback is welcome

brn added a commit to brn/commander.js that referenced this issue Aug 3, 2017
brn added a commit to brn/commander.js that referenced this issue Aug 3, 2017
brn added a commit to brn/commander.js that referenced this issue Aug 3, 2017
kirrg001 added a commit to TryGhost/knex-migrator that referenced this issue Aug 6, 2018
no issue

- `--version` is a reserved option (see tj/commander.js#648). Same applies to `--name` etc.
- We have to use `--v` for now.
- This is in theory a breaking change, but the feature never worked when using `knex-migrator rollback --version x.x.x` in the shell
  - that's why it's a bug fix
- updated docs
@icosahedron
Copy link

icosahedron commented Sep 21, 2018

I worked around this limitation with the following (TypeScript, but probably similar in vanilla JS):

import * as cmd from 'commander'

cmd
  .version('0.0.1')

cmd.optionValues = {}

cmd
  .command('start <duration>')
  .alias('st')
  .description('start session for <duration>')
  .option('-n, --name [name]', 'name of the timer')
  .option("-t, --task [task description]", "description of task for timer")
  .action(function(duration){
    console.log(duration);
    console.log(cmd.optionValues);
  })
  .on('--help', function() {
      console.log("\nduration must be a number followed by an m for minutes (20m) or s for seconds (120s).\n")
  })
  .on('option:name', function(opt) {
      cmd.optionValues.name = opt;
  })
  .on('option:task', function(opt) {
      cmd.optionValues.task = opt;
  });

kevinpollet added a commit to kevinpollet/create-node-typescript that referenced this issue Feb 3, 2019
@shadowspawn
Copy link
Collaborator

Closing this as covered by #183 and #933

@shadowspawn
Copy link
Collaborator

shadowspawn commented Nov 23, 2019

I have opened a Pull Request which allows storing option values separately rather than as command properties (access using .opts()), and passes the options (rather than the command) to the action handler.

See #1102

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

4 participants