3

I'm working with yargs to pass arguments in a gulp command. I am having troubles checking the validity of the arguments and what shall happen if the flag is present or not.

More specifically, here is my gulpfile.js:

'use strict';

var argv = require('yargs').options({
    'config': {
        alias: 'c',
        demandOption: true,
        default: 'default',
        describe: 'Choose a configuration file name',
        type: 'string'
      },
    'host': {
        alias: 'h',
        demandOption: false,
        default: '',
        describe: 'Replace the host starting with http://',
        type: 'string'
    }
}).argv;

gulp.task('config', function(){

// If argument -c is passed copy config file in path 
// configs/Config_{{argv.c}}.js into Config.js
    gulp.src('./app/jsx/constants/Config_' + argv.c + '.js')
      .pipe(rename({ basename: 'Config'}))
      .pipe(gulp.dest('./app/jsx/constants'))
})

If I gulp config -c something, everything works as expected.

What I would like to have is : having the CLI asking for a config argument if not provided in the command.

Has anyone had experience with that?

Lucbug
  • 475
  • 6
  • 16

1 Answers1

3

It took me a little while to figure out that commenting out your default options within the config object makes it work.

It does make sense that if the demandOption is set to true that there should be no default! The documentation however doesn't appear to say anything about it (and it just fails silently), see valid options keys.

Consistent with this theory is that your second not-required option, 'host' works fine with even an undefined or null default: "".

Mark
  • 143,421
  • 24
  • 428
  • 436