1 | **💛 You can help the author become a full-time open-source maintainer by [sponsoring him on GitHub](https://github.com/sponsors/egoist).**
|
2 |
|
3 | ---
|
4 |
|
5 | # tinyargs
|
6 |
|
7 | [![npm version](https://badgen.net/npm/v/tinyargs)](https://npm.im/tinyargs) [![npm downloads](https://badgen.net/npm/dm/tinyargs)](https://npm.im/tinyargs) [![Coverage Status](https://coveralls.io/repos/github/egoist/tinyargs/badge.svg?branch=main)](https://coveralls.io/github/egoist/tinyargs?branch=main)
|
8 |
|
9 | A tiny and flexible command-line argument parser for Node.js and Deno.
|
10 |
|
11 | ## Features
|
12 |
|
13 | - Support combined short flags, `-abc foo` is expanded to `-a -b -c foo`
|
14 | - Support using equal sign to specify argument value, `-c=config.js` is expanded to `-c config.js`
|
15 | - Support positional arguments like `your-cli foo bar`
|
16 | - Support collecting trailing arguments
|
17 | - Support sub commands
|
18 |
|
19 | ## Install
|
20 |
|
21 | ```bash
|
22 | npm i tinyargs
|
23 | ```
|
24 |
|
25 | [Deno](https://deno.land) users:
|
26 |
|
27 | ```ts
|
28 | import { parse } from "https://deno.land/x/tinyargs/mod.ts"
|
29 | ```
|
30 |
|
31 | ## Examples
|
32 |
|
33 | ### Simple Example
|
34 |
|
35 | ```ts
|
36 | import { parse } from "tinyargs"
|
37 |
|
38 | const cli = parse(process.argv.slice(2), [
|
39 | { name: "help", flags: ["h"], type: Boolean, stop: true },
|
40 | { name: "version", flags: ["v"], type: Boolean, stop: true },
|
41 | { name: "files", type: String, positional: true, multiple: true },
|
42 | ])
|
43 |
|
44 | if (cli.help) {
|
45 | console.log(`...your help message`)
|
46 | process.exit()
|
47 | }
|
48 |
|
49 | if (cli.version) {
|
50 | console.log(`...version number`)
|
51 | process.exit()
|
52 | }
|
53 |
|
54 | console.log(cli.files)
|
55 | ```
|
56 |
|
57 | Run this cli:
|
58 |
|
59 | ```bash
|
60 | $ node cli.js -h
|
61 | ...your help message
|
62 |
|
63 | $ node cli.js -v
|
64 | ...version number
|
65 |
|
66 | $ node cli.js foo.js bar.js
|
67 | [ 'foo.js', 'bar.js' ]
|
68 |
|
69 | $ node cli.js
|
70 | error: missing positional argument: files
|
71 | ```
|
72 |
|
73 | If `-h, --help` or `-v, --version` appears, the remaining arguments are not parsed, since we added `stop: true` to the option.
|
74 |
|
75 | By default all options and positional arguments are required to have a value, if you add a string option named `foo` but it's used like `--foo --bar`, it will throw an error. This can be customized by setting `optionalValue: true`, which in this case would give `foo` a default value of `true` instead.
|
76 |
|
77 | ### Sub Commands
|
78 |
|
79 | Create a CLI with two sub commands: (<small>_We use `<>` and `[]` to denote cli arguments in the docs, `<>` means it's required, `[]` means it's optional._</small>)
|
80 |
|
81 | - `run <script> [args...]`: like `yarn run` or `npm run`, run a script and collect trailing arguments so that you can pass them to the script later.
|
82 | - `download <url>`: download from a url.
|
83 |
|
84 | ```ts
|
85 | const cli = parse(process.argv.slice(2), [
|
86 | { name: "command", type: String, positional: true },
|
87 | {
|
88 | name: "script",
|
89 | type: String,
|
90 | positional: true,
|
91 | when: (cli) => cli.command === "run",
|
92 | stop: true,
|
93 | },
|
94 | {
|
95 | name: "url",
|
96 | type: String,
|
97 | positional: true,
|
98 | when: (cli) => cli.command === "download",
|
99 | },
|
100 | ])
|
101 |
|
102 | if (cli.command === "run") {
|
103 | console.log(`...running ${cli.script} with forwarded arguments ${cli._}`)
|
104 | } else if (cli.command === "download") {
|
105 | console.log(`...downloading ${cli.url}`)
|
106 | } else {
|
107 | console.log(`...unknown command ${cli.command}`)
|
108 | }
|
109 | ```
|
110 |
|
111 | ## Guide
|
112 |
|
113 | ### Flags
|
114 |
|
115 | Define a flag that could be used as `your-cli --file <value>`, only `name` and `type` are required:
|
116 |
|
117 | ```ts
|
118 | parse(process.argv.slice(2), [{ name: "file", type: String }])
|
119 | ```
|
120 |
|
121 | ### Positional Arguments
|
122 |
|
123 | Define a positional argument that could be used as `your-cli <file>`, by setting `positional: true`:
|
124 |
|
125 | ```ts
|
126 | parse(process.argv.slice(2), [{ name: "file", type: String, positional: true }])
|
127 | ```
|
128 |
|
129 | ### Repeated / Multiple Flags and Positional Arguments
|
130 |
|
131 | Flags can be repeated if you set `multiple: true`:
|
132 |
|
133 | ```ts
|
134 | parse(process.argv.slice(2), [{ name: "file", type: String, multiple: true }])
|
135 | ```
|
136 |
|
137 | Now your cli can be used as `your-cli --file a.js --file b.js`, the resulting object will look like: `{ file: [ 'a.js', 'b.js' ] }`.
|
138 |
|
139 | Positional arguments work in a similar fashion:
|
140 |
|
141 | ```ts
|
142 | parse(process.argv.slice(2), [
|
143 | { name: "file", type: String, positional: true, multiple: true },
|
144 | ])
|
145 | ```
|
146 |
|
147 | Now you can do `your-cli a.js b.js`, the resulting object will look like: `{ file: [ 'a.js', 'b.js' ] }`.
|
148 |
|
149 | Note that you can't have two `multiple` `poitional` arguments in the same command, because the first one will collect all positional arguments.
|
150 |
|
151 | ### Collecting Trailing Arguments
|
152 |
|
153 | There're two way, first is to use the `stop` option, which will stop parsing arguments after the option, and arguments beyond that point will be collected under `_` in the returned object:
|
154 |
|
155 | ```ts
|
156 | const cli = parse(
|
157 | ["--foo", "bar", "baz", "--some-flag"],
|
158 | [{ name: "foo", type: Boolean, stop: true }],
|
159 | )
|
160 |
|
161 | console.log(cli)
|
162 | // { foo: true, _: [ 'bar', 'baz', '--some-flag' ] }
|
163 | ```
|
164 |
|
165 | The second way is to use `positional: true` with `multiple: 'include-flags'`, which will collect trailing arguments into the specific option:
|
166 |
|
167 | ```ts
|
168 | const cli = parse(
|
169 | ["--foo", "bar", "baz", "--some-flag"],
|
170 | [
|
171 | { name: "foo", type: Boolean },
|
172 | {
|
173 | name: "args",
|
174 | type: String,
|
175 | positional: true,
|
176 | multiple: "include-flags",
|
177 | },
|
178 | ],
|
179 | )
|
180 |
|
181 | console.log(cli)
|
182 | // { foo: true, args: [ 'bar', 'baz', '--some-flag' ] }
|
183 | ```
|
184 |
|
185 | ## API Reference
|
186 |
|
187 | https://www.jsdocs.io/package/tinyargs
|
188 |
|
189 | ## Sponsors
|
190 |
|
191 | [![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)
|
192 |
|
193 | ## License
|
194 |
|
195 | MIT © [EGOIST](https://github.com/sponsors/egoist)
|