【问题标题】:Using Yargs inside Gulp在 Gulp 中使用 Yargs
【发布时间】:2018-04-11 17:00:54
【问题描述】:

我正在使用 yargs 在 gulp 命令中传递参数。我在检查参数的有效性以及标志是否存在时会发生什么问题。

更具体地说,这是我的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'))
})

如果我gulp config -c something,一切都会按预期进行。

我想要的是:让 CLI 请求配置参数(如果命令中未提供)。

有人有这方面的经验吗?

【问题讨论】:

    标签: javascript gulp yargs


    【解决方案1】:

    我花了一点时间才弄清楚,在 config 对象中注释掉你的 default 选项可以让它工作。

    如果demandOption 设置为true 则应该没有默认值,这确实是有道理的!然而,文档似乎没有说明任何内容(它只是默默地失败了),请参阅valid options keys

    与此理论一致的是,您的第二个非必需选项“host”即使是未定义或空的default: "" 也能正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多