0

I have a Grunt task and if the --verbose command line flag is on, I want to echo some more info to the console.

Finding out if that flag is on isn't possible with grunt.option('verbose'). It also doesn't appear to be anywhere in grunt.package.

How can I see, from within a task, if the user has the verbose flag set?

Eric
  • 5,104
  • 10
  • 41
  • 70
  • Possible duplicate of [How do I pass command line arguments?](https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments) – Pavel Hasala Jul 12 '17 at 12:44
  • I posted an answer for you, but also flagged you question as dup of https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments/4351548 – Pavel Hasala Jul 12 '17 at 12:44

1 Answers1

1

That is imho because grunt.option works only for a grunt used flags, it doesnt take every shell argument you provide.

The esiest module-free solution is to parse your flag from process.argv, that returns an array. Your flags would start at position 2, so if --verbose is the first argument, you would claim it by process.argv[2] How do I pass command line arguments?

You can easily test it by creating a javascript file

  var args = process.argv;
  process.argv.forEach( (val, index, array) => {
        var flag = val.replace(new RegExp('-', 'g'), '');
        console.log(flag);
  });

and calling it in your shell

node testParams.js --argument1 -t. 

The outcome will look like this

hakim@cortana:~/Sites/DOODLINGS $ node testParams.js --verbose -t
/usr/local/Cellar/node/7.5.0/bin/node
/Users/hakim/Sites/DOODLINGS/testParams.js
verbose
t

With some googling, you can find module to extract params for you. I dont use grunt so cant help you much there.

Pavel Hasala
  • 946
  • 10
  • 14