加入使用指南,改进命令行脚本

如果你在日常编程冒险中已经熟悉JavaScript,当你需要写一个快速命令行脚本时,你就不会太震惊了。快速脚本往往有点脏,通常缺乏使用文档。

开始的

要开始使用命令行使用,您需要通过您喜爱的包管理器添加它:

1# npm
2$ npm install command-line-usage --save
3
4# Yarn
5$ yarn add command-line-usage

请确保导入命令行使用:

1const cliUsage = require('command-line-usage');

虽然本文专注于命令行使用包,但值得注意的是,该包利用了chalk模板的字面语法,并与chalk方法(如chalk.red())玩得相当好。

如果您更喜欢使用chalk方法而不是模板的字面语法,请确保也导入chalk,这是一种命令行使用的依赖性:

1const chalk = require('chalk');

若要更深入地了解chalk,请参阅我们的文章 Styling Output from Command-line Node.js Script with Chalk

基本使用

有了所有安装和正确的进口,我们可以构建一些简单的使用信息:

 1const sections = [
 2  {
 3    header: '🐊 Alligator.io CLI Script',
 4    content: 'From {bold your friends} at {underline Alligator.io}',
 5  },
 6  {
 7    header: 'Usage',
 8    content: [
 9      '% script {bold --file} {underline /path/to/file} ...',
10    ],
11  },
12];
13
14const usage = cliUsage(sections);
15console.info(usage);

数组包含您希望在您的使用信息中显示的信息的组合,您可以在每个节中有不同的选项,您可以有尽可能多的节。

通过命令行使用运行部分数组将生成一个字符串,然后我们可以登录到控制台。

论点列表

要列出脚本的参数,您可以使用optionList属性,该属性需要一系列选项:

 1const sections = [
 2  {
 3    header: 'Mandatory Options',
 4    optionList: [
 5      {
 6        name: 'file',
 7        alias: 'f',
 8        type: String,
 9        typeLabel: '{underline /path/to/file}',
10        description: 'The file to do stuff with',
11      },
12    ],
13  },
14  {
15    header: 'Optional Stuff',
16    optionList: [
17      {
18        name: 'letters',
19        alias: 'l',
20        type: String,
21        typeLabel: '{underline letter} ...',
22        description: 'This option takes multiple values',
23        multiple: true,
24        defaultOption: 'a b c',
25      },
26      {
27        name: 'help',
28        alias: '?',
29        type: Boolean,
30        description: 'Print this usage guide.',
31      }
32    ],
33  },
34];
35
36const usage = cliUsage(sections);
37console.info(usage);

命令行使用包通过在表布局中分隔参数来处理所有艰苦的工作。

桌面布局

谈到在演示参数列表时使用的表格布局,你也可以使用表格布局将内容属性传递到一个数组时。

 1const sections = [
 2  {
 3    header: 'Table Layout Example',
 4    content: [
 5      {
 6        desc: 'More great Alligator.io articles:',
 7        url: '{underline https://alligator.io}',
 8      },
 9      {
10        desc: '`command-line-usage` on GitHub:',
11        url: '{underline https://git.io/fh9TQ}',
12      },
13    ],
14  },
15];
16
17const usage = cliUsage(sections);
18console.info(usage);

有点乐趣

有时你只是想有点乐趣,正如前面所提到的,命令行使用可以利用``,这意味着你可以用一种颜色来调味你的使用输出。

为了变得更疯狂,如果你要将你的内在Demoscene艺术家渠道,并将一些ASCII / ANSI艺术组合在一起,用逃避代码,你会想要包括原始属性,以确保事物被正确显示。

 1const sections = [
 2  {
 3    header: 'Raw Like Sushi',
 4    raw: true,
 5    content: [
 6      '{red   ,iiiiiiiiii,}',
 7      '{red ,iiiiiiiiiiiiii,}',
 8      `{red iii'        'ii'}`,
 9      `{white   '.________.'}`,
10    ],
11  },
12];
13
14const usage = cliUsage(sections);
15console.info(usage);

🍣

结论

尽管命令行使用包使创建脚本使用信息变得容易,但它仍然有些缺乏。

命令行参数的实际处理要么需要通过自行解析参数矢量 process.argv 来滚动自己的解决方案来处理,要么需要利用一个包,例如 `command'

有关更多信息,请参阅我们的文章 处理 Node.js 脚本中的命令行论点

Published At
Categories with 技术
Tagged with
comments powered by Disqus