UNPKG

2.87 kBMarkdownView Raw
1# argue-cli
2
3[![NPM version][npm]][npm-url]
4[![Node version][node]][node-url]
5[![Dependencies status][deps]][deps-url]
6[![Build status][build]][build-url]
7[![Coverage status][coverage]][coverage-url]
8[![Bundle size][size]][size-url]
9
10[npm]: https://img.shields.io/npm/v/argue-cli.svg
11[npm-url]: https://www.npmjs.com/package/argue-cli
12
13[node]: https://img.shields.io/node/v/argue-cli.svg
14[node-url]: https://nodejs.org
15
16[deps]: https://img.shields.io/librariesio/release/npm/argue-cli
17[deps-url]: https://libraries.io/npm/argue-cli/tree
18
19[build]: https://img.shields.io/github/workflow/status/TrigenSoftware/Argue/CI.svg
20[build-url]: https://github.com/TrigenSoftware/Argue/actions
21
22[coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/Argue.svg
23[coverage-url]: https://app.codecov.io/gh/TrigenSoftware/Argue
24
25[size]: https://img.shields.io/bundlephobia/minzip/argue-cli
26[size-url]: https://bundlephobia.com/package/argue-cli
27
28A thin and strongly typed CLI arguments parser for Node.js.
29
30## Usage
31
321. Install
33
34```bash
35# yarn
36yarn add argue-cli
37# pnpm
38pnpm add argue-cli
39# npm
40npm i argue-cli
41```
42
432. Import in your code and use it!
44
45```ts
46import { read, end, expect, alias, option, readOptions } from 'argue-cli'
47
48/**
49 * Expect and read one of the commands
50 */
51const command = expect(
52 alias('install', 'i'),
53 'remove'
54)
55let options = {}
56
57if (command === 'install') {
58 /**
59 * Read passed options
60 */
61 options = readOptions(
62 option(alias('save', 'S'), Boolean),
63 option(alias('saveDev', 'save-dev', 'D'), Boolean),
64 option('workspace', String)
65 )
66}
67
68/**
69 * Read next argument
70 */
71const packageName = read()
72
73/**
74 * Expect end of the arguments
75 */
76end()
77
78/* ... */
79```
80
81## API
82
83<table>
84 <thead>
85 <tr>
86 <th>Method</th>
87 <th>Description</th>
88 </tr>
89 </thead>
90 <tbody>
91 <tr>
92<td>
93
94```ts
95function read(): string
96```
97
98</td>
99<td>
100 Read next argument. Throws error if no next argument.
101</td>
102 </tr>
103 <tr>
104<td>
105
106```ts
107function end(): void
108```
109
110</td>
111<td>
112 Expectation of the end. Throws an error if there are more arguments left.
113</td>
114 </tr>
115 <tr>
116<td>
117
118```ts
119function expect(...argRefs: ArgRef[]): string
120```
121
122</td>
123<td>
124 Expect one of the given arguments.
125</td>
126 </tr>
127 <tr>
128<td>
129
130```ts
131function alias(name: string, ...aliases: string[]): AliasArgRef
132```
133
134</td>
135<td>
136 Describe argument with aliases.
137</td>
138 </tr>
139 <tr>
140<td>
141
142```ts
143function option(argRef: ArgRef, type: PrimitiveConstructor): OptionReader
144```
145
146</td>
147<td>
148 Describe option with value.
149</td>
150 </tr>
151 <tr>
152<td>
153
154```ts
155function readOptions(...optionReaders: OptionReader[]): OptionResult
156```
157
158</td>
159<td>
160 Read options from arguments.
161</td>
162 </tr>
163 </tbody>
164</table>
165
166## TypeScript
167
168In [API section](#API) types are described in a simplified way. Detailed example of the types you can see [here](test/argue.test-d.ts).