Node js commander args returns true instead the value
Asked Answered
D

3

28

I want to create a script with node and node commander and when i try to grab the values of my args i get the value true instead the value itself.

For example if i write this in terminal:

node myfile.js -s somefile -d test

var program = require('commander');

program
  .version('0.0.1')
  .option('-s, --src', 'src csv file')
  .option('-d, --destination', 'destination csv file')
  .parse(process.argv);

console.log(program.src); // return true
console.log(program.destination); // return true

How i get the value of this args?

Davedaveda answered 22/2, 2015 at 19:30 Comment(1)
Can you please include the output from: console.log(JSON.stringify(program)); Thanks.Tonality
S
37

The documentation isn't very clear and only shows this by example, but the syntax you have used ('-s, --src') is for boolean values.

If you want to take a string you need to say so: '-s, --src <item>'

Simmon answered 22/2, 2015 at 20:10 Comment(0)
T
12

You aren't specifying that the options take input. You can do this by including <data> or [data] (if optional) in your option definitions:

.option('-s, --src <src>', 'src csv file') .option('-d, --dest [dest]', 'destination csv file')

Tonality answered 22/2, 2015 at 20:15 Comment(0)
C
8

You need to specify that the options take an argument and that they are not just a flag

program
  .version('0.0.1')
  .option('-s, --src <file>', 'src csv file')
  .option('-d, --destination <file>', 'destination csv file')
  .parse(process.argv);

If the argument is mandatory, it is specified with <>. If it is optional, with [].

Cameroun answered 22/2, 2015 at 20:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.