nodejs / nodejs/node

Add support to print help/usage of util.parseArgs

未关闭
#56,184 8 条评论 16 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

feature request
主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
283

描述

What is the problem this feature will solve?

An almost ubiquitous practice of writing a CLI is including a help command in the CLI itself. Currently anyone using util.parseArgs needs to write their own help command. This, in my opinion, makes parseArgs incomplete.

What is the feature you are proposing to solve the problem?

My suggestion is to include the ability to prin help/usage information generated directly from the config given to parseArgs. A minimum would be something along the lines of:

  1. Include help text for each ParseArgsOptionConfig to include help text for each option in particula:
foo: {    
 type: 'boolean',    
 short: 'f',   
 help: 'enable foo',
},
  1. Add a main help text to the ParseArgsConfig as well, to include details on what the CLI as a whole:
parseArgs({ 
  help: 'Use this command to achieve foo',
  options: {
    // ...
  }
});
  1. Add the --help/-h option to print the generated usage information either:
  • always add the --help/-h option, and automatically print the usage and exit the process when parsing the args (E.g. how python's builtin argparse does it); this would be a breaking change since it may conflict with other similar options implemented already.
  • add a new option to the ParseArgsConfig to enable autogenerated help option if desired
parseArgs({ 
  enableHelp: true,
  help: 'Use this command to achieve foo',
  options: {
    // ...
  }
});
  • simply provide a method to print the usage and let the user implement it themselves in userland
const { values, positionals, printUsage } = parseArgs({
  help: 'Use this command to achieve foo',
  options: {
    help: {
      type: "boolean",
      short: "h",
      help: "print command line options and exit"
    }
  }
});
if (values.help) {
  printUsage();
  process.exit(0);
}
What alternatives have you considered?

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 util.parseArgs 入口点开始,检查现有的 ParseArgsConfig 和 ParseArgsOptionConfig 行为。确定应支持哪种帮助生成 API 以及 --help/-h 行为;当选定的设计已针对命令和选项的用法文本实现时,这项工作就完成了。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
cli
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
冷清
描述清晰度
需要澄清
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。