1 | # commandpost [![Circle CI](https://circleci.com/gh/vvakame/commandpost.png?style=badge)](https://circleci.com/gh/vvakame/commandpost)
|
2 |
|
3 | commandpost is a command-line options parser.
|
4 | This library is inspired by [commander](https://www.npmjs.com/package/commander).
|
5 |
|
6 | commander is very user-friendly, but not [TypeScript](https://www.npmjs.com/package/typescript)-friendly.
|
7 | commandpost aims to improve this.
|
8 | Of course, commandpost can also be used from an ordinary JavaScript program. :+1:
|
9 |
|
10 | ## Installation
|
11 |
|
12 | ```
|
13 | $ npm install --save commandpost
|
14 | ```
|
15 |
|
16 | ## How to Use
|
17 |
|
18 | ### Basic Usage
|
19 |
|
20 | ```
|
21 | $ cat cli.ts
|
22 | import * as commandpost from "commandpost";
|
23 |
|
24 | let root = commandpost
|
25 | .create<{ spice: string[]; }, { food: string; }>("dinner <food>")
|
26 | .version("1.0.0", "-v, --version")
|
27 | .description("today's dinner!")
|
28 | .option("-s, --spice <name>", "What spice do you want? default: pepper")
|
29 | .action((opts, args) => {
|
30 | console.log(`Your dinner is ${args.food} with ${opts.spice[0] || "pepper"}!`);
|
31 | });
|
32 |
|
33 | commandpost
|
34 | .exec(root, process.argv)
|
35 | .catch(err => {
|
36 | if (err instanceof Error) {
|
37 | console.error(err.stack);
|
38 | } else {
|
39 | console.error(err);
|
40 | }
|
41 | process.exit(1);
|
42 | });
|
43 |
|
44 | $ node cli.js --help
|
45 | Usage: dinner [options] [--] <food>
|
46 |
|
47 | Options:
|
48 |
|
49 | -s, --spice <name> What spice do you want? default: pepper
|
50 |
|
51 | $ node cli.js -s "soy sause" "fillet steak"
|
52 | Your dinner is fillet steak with soy sause!
|
53 | ```
|
54 |
|
55 | ### Commands
|
56 |
|
57 | A top-level command is created by the `commandpost.create` function.
|
58 |
|
59 | commandpost also supports sub-commands.
|
60 | A sub-command is created by using the `topLevelCommand.subCommand` method.
|
61 | Refer to [this](https://github.com/vvakame/commandpost/blob/master/example/usage.ts#L36) example for a demonstration.
|
62 |
|
63 | commandpost can automatically generate help and command usage messages based on your configuration. For best results, it is recommended that you should set `.version` and `.description` for your top-level command.
|
64 |
|
65 |
|
66 | ### Options
|
67 |
|
68 | ```
|
69 | // shorthand & formal option with a required parameter. value is converted to string[].
|
70 | cmd.option("-c, --config <configFile>", "Read setting from specified config file path");
|
71 |
|
72 | // option with optional parameter. value is converted to string[].
|
73 | cmd.option("-c, --config [configFile]", "Read setting from specified config file path");
|
74 |
|
75 | // option without parameter (flag). option value is converted to boolean and defaults to `false`.
|
76 | cmd.option("--suppress-warning", "Suppress warning");
|
77 |
|
78 | // option with `--no-` prefix. option value is converted to boolean and defaults to true.
|
79 | cmd.option("--no-type-checking", "Type checking disabled");
|
80 | ```
|
81 |
|
82 | If you want to handle unknown options, you can use the `.allowUnknownOption` method.
|
83 |
|
84 | ### Arguments
|
85 |
|
86 | ```
|
87 | // required argument
|
88 | commandpost.create<{}, { food: string; }>("dinner <food>");
|
89 |
|
90 | // optional argument
|
91 | commandpost.create<{}, { food: string; }>("dinner [food]");
|
92 |
|
93 | // variadic argument
|
94 | commandpost.create<{}, { foods: string[]; }>("dinner <food...>");
|
95 | ```
|
96 |
|
97 | ### Examples
|
98 |
|
99 | * [example](https://github.com/vvakame/commandpost/blob/master/example/usage.ts) dir
|
100 | * [typescript-formatter](https://github.com/vvakame/typescript-formatter/blob/master/lib/cli.ts)
|
101 | * [dtsm](https://github.com/vvakame/dtsm/blob/master/lib/cli.ts)
|
102 | * [review.js](https://github.com/vvakame/review.js/blob/master/lib/cli.ts)
|
103 | * [prh](https://github.com/vvakame/prh/blob/master/lib/cli.ts)
|
104 |
|
105 | ## Contributing
|
106 |
|
107 | This package's author, vvakame, is not a native English speaker. My first language is Japanese.
|
108 | If you find incorrect English, please send me a pull request.
|