简体   繁体   English

如何使用 yargs 获取除 '.$0' 和 '._' 之外的所有参数

[英]How to get all params exept '.$0' and '._' using yargs

When I am using yargs module to simplify params handling当我使用 yargs 模块来简化参数处理时

Yargs always reruns params as an object, here is an example, if I run this command node index.js default --optipn1=true --option2=false --custom Yargs 总是将参数重新运行为 object,这是一个示例,如果我运行此命令node index.js default --optipn1=true --option2=false --custom

Yargs return this array: Yargs 返回这个数组:

{
  _: [ 'default' ],
  optipn1: 'true',
  option2: 'false',
  custom: true,
  '$0': 'index.js'
}

What I want is the same array of params excluding _ and $0 ,我想要的是相同的参数数组,不包括_$0

{
  optipn1: 'true',
  option2: 'false',
  custom: true,
}

is there anyway to do that that?有没有办法那样做?

Is there any built-in functionality to achieve that?是否有任何内置功能可以实现这一目标?

 const obj = { _: [ 'default' ], optipn1: 'true', option2: 'false', custom: true, '$0': 'index.js' }; let newObj = {}; for(let [key, value] of Object.entries(obj)) { if(key.= '_' && key.= '$0') { newObj = {.,:newObj. [`${key}`]: value} } } console.log(newObj)

Here we go;) I hope be useful for you我们这里go;)希望对你有用

let obj = {
  _: ['default'],
  optipn1: 'true',
  option2: 'false',
  custom: true,
  '$0': 'index.js'
};
let {_, $0, ...targetObj} = obj;
console.log(targetObj);

Javascript Javascript

const configuration = yargs.argv;
delete configuration._;
delete configuration.$0;

console.log(configuration);

Typescript Typescript

import yargs from 'yargs';

const configuration: Record<string, unknown> = yargs.parseSync();
delete configuration._;
delete configuration.$0;

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

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