简体   繁体   English

使用 yargs 将默认数值添加到可选参数

[英]add default numerical value to optional argument with yargs

I have a cli script that accepts commandline parameters.我有一个接受命令行参数的 cli 脚本。
For the pages parameter I would like the behaviour to be:对于 pages 参数,我希望行为是:

> script.js

-> pages parameter not set. -> 页面参数未设置。 don't process pages不处理页面

> script.js -p 100

-> pages parameter set with value 100: process 100 pages -> pages 参数设置为 100:处理 100 页

> script.js -p

-> pages parameter set without value: process infinite (however much available) pages -> pages 参数集没有值:处理无限(无论有多少可用)页面

In other words: I want to give an optional parameter a default value.换句话说:我想给一个可选参数一个默认值。 The problem is that I can't figure out how to distinguish between the situation where it's set by the user without value and when it's not set.问题是我不知道如何区分用户设置的没有值的情况和未设置的情况。

When defined like this:当这样定义时:

  var argv = require('yargs')
  // omitted code
  .alias('p', 'pages')
  .describe('p', 'number of pages; infinite by default')
  .default('p', -1)
  .number('p')

argv.pages is always -1 whether I set -p or not无论我是否设置-p argv.pages总是-1

And when defined like this:当这样定义时:

  .alias('p', 'pages')
  .describe('p', 'number of pages; infinite by default')
  .number('p')

typeof argv.pages is always undefined , whether I set -p or not typeof argv.pages总是undefined ,无论我是否设置-p

How can I give a default value while treating the parameter as an optional argument;如何在将参数视为可选参数时给出默认值; so, no default when it's not set and only a default when it's set without value.所以,没有设置时没有默认值,只有在没有设置值时才设置默认值。


(of course there is a workaround : I could manually parse the arguments by looping through process.argv, but I want to avoid that as that defeats the purpose of using yargs) (当然有一个解决方法:我可以通过循环 process.argv 来手动解析参数,但我想避免这种情况,因为这违背了使用 yargs 的目的)

I don't believe yargs has a method for something like this, I thought of using #coerce() , but that function gets called regardless if the command line has -p or not, so it wouldn't work (unless you checked for '-p' and '--pages' in the process.argv).我不相信 yargs 有这样的方法,我想使用#coerce() ,但是无论命令行是否有-p都会调用该函数,因此它不起作用(除非您检查process.argv 中的“-p”和“--pages”)。

One solution would be to remove the default and modify it after yargs is done.一种解决方案是删除默认值并在 yargs 完成后修改它。 If you remove the default and specify -p , in argv it will just be undefined.如果删除默认值并指定-p ,则在 argv 中它将是未定义的。 If you don't specify -p at all it won't be a property at all.如果您根本不指定-p ,则它根本不会是属性。

script -p 100 // => { _: [], $: "", pages: 100, p: 100 }
script -p // => { _: [], $: "" pages: undefined, p: undefined }
script // => { _: [], $: "" }

Now we can check if pages exists in the object but is undefined, and then do whatever:现在我们可以检查对象中是否存在页面但未定义,然后执行任何操作:

var argv = require('yargs')
    // omitted code
    .alias('p', 'pages')
    .describe('p', 'number of pages; infinite by default')
    .number('p').argv;

if ('pages' in argv && typeof argv.pages === 'undefined') {
    // set to whatever you want
    argv.pages = Infinity;
    argv.p = Infinity;
}

console.log(argv);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM