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

ocebsuys  于 2022-12-26  发布在  Node.js
关注(0)|答案(3)|浏览(101)

当我使用yargs模块来简化参数处理时
Yargs总是将params作为对象重新运行,这里是一个示例,如果我运行node index.js default --optipn1=true --option2=false --custom命令
Yarg返回此数组:

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

我需要的是相同的参数数组,除了_$0

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

有办法做那件事吗?
是否有任何内置功能可以实现这一点?

h43kikqp

h43kikqp1#

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)

开始了我希望对你有用

jucafojl

jucafojl2#

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

o7jaxewo3#

Javascript语言

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

console.log(configuration);

typescript

import yargs from 'yargs';

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

相关问题