You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-46Lines changed: 62 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ After writing countless CLI utilities, it became clear that the majority of most
12
12
> **This tool is just a parser.** It parses arguments and optionally enforces developer-defined rules. It exposes all relevant aspects of the arguments so developers can use the parsed content in any manner. It does not attempt to autogenerate help screens or apply any other "blackbox" functionality. WYSIWYG.<br/>
13
13
**If your tool needs more management/organization features, see the [@author.io/shell](https://github.com/author/shell) micro-framework**_(which is built atop this library)_**.**
14
14
15
-
## Example
15
+
## Verbose Example
16
16
17
17
**Install:**`npm install @author.io/arg`
18
18
@@ -73,7 +73,7 @@ _Output:_
73
73
}
74
74
```
75
75
76
-
## Even Simpler Syntax
76
+
## Simpler Syntax
77
77
78
78
For brevity, there is also a `configure` method which will automatically do all of the things the first script does, but with minimal code.
79
79
@@ -125,35 +125,15 @@ console.log(Args.data)
125
125
126
126
## API/Usage
127
127
128
-
The source code is pretty easy to figure out, but here's what's possible:
128
+
The source code is pretty easy to figure out, but here's an overview:
129
129
130
-
### Properties
130
+
##Configuring Parser Logic
131
131
132
-
-`flags`: An array of the unique flag names passed to the application.
133
-
-`data`: A key/value object representing all data passed to the application. If a flag is passed in more than once and duplicates are _not_ suppressed, the value will be an array.
134
-
-`length` The total number of arguments passed to the application.
135
-
-`valid` A boolean representing whether all of the validation rules passed or not.
136
-
-`violations` An array of violations (this is an empty array when everything is valid).
137
-
138
-
## Configuration Methods
139
-
140
-
The main methods are used to configure rules.
141
-
142
-
The following attributes configuration attributes can be set for each flag:
143
-
144
-
-`required` - Indicates the flag must be present in the command.
145
-
-`default` - A value to use when the flag is not specified.
146
-
-`type` - The data type. Supports primitives like `Boolean` or their text (typeof) equivalent (i.e. "`boolean`").
147
-
-`alias` - A string representing an alternative name for the flag.
148
-
-`aliases` - Support for multiple aliases.
149
-
-`allowMultipleValues` - If a flag is specified more than once, capture all values (instead of only the last one specified).
150
-
-`options` - An array of valid values for the flag.
151
-
152
-
### configure({...})
132
+
There are two ways to configure the parser. A single `configure()` method can describe everything, or individual methods can be used to dynamically define the parsing logic.
153
133
154
-
This method accepts a configuration, which will automatically invoke all of the appropriate configuration methods using a shorthand syntax.
134
+
### Using `configure()`
155
135
156
-
For example:
136
+
The `configure()` method accepts a shorthand (yet-easily-understood) configuration object.
157
137
158
138
```javascript
159
139
Args.configure({
@@ -170,37 +150,51 @@ Args.configure({
170
150
})
171
151
```
172
152
173
-
### require('flag1', 'flag2', ...)
153
+
_Purpose:_
154
+
155
+
-`required` - Indicates the flag must be present in the command.
156
+
-`default` - A value to use when the flag is not specified.
157
+
-`type` - The data type. Supports primitives like `Boolean` or their text (typeof) equivalent (i.e. "`boolean`").
158
+
-`alias` - A string representing an alternative name for the flag.
159
+
-`aliases` - Support for multiple aliases.
160
+
-`allowMultipleValues` - If a flag is specified more than once, capture all values (instead of only the last one specified).
161
+
-`options` - An array of valid values for the flag.
162
+
163
+
### Using Individual Methods
164
+
165
+
The following methods can be used to dynamically construct the parsing logic, or modify existing logic.
166
+
167
+
#### require('flag1', 'flag2', ...)
174
168
175
169
Require the presence of specific flags amongst the arguments. Automatically executes `recognize` for all required flags.
176
170
177
-
### recognize('flag1', 'flag2', ...)
171
+
####recognize('flag1', 'flag2', ...)
178
172
179
173
Register "known" flags. This is useful when you want to prevent unrecognized flags from being passed to the application.
180
174
181
-
### types({...})
175
+
####types({...})
182
176
183
177
Identify the data type of a flag or series of flags. Automatically executes `recognize` for any flags specified amongst the data types.
184
178
185
-
### defaults({...})
179
+
####defaults({...})
186
180
187
181
Identify default values for flags.
188
182
189
183
Automatically executes `recognize` for any flags specified amongst the defaults.
190
184
191
-
### alias({...})
185
+
####alias({...})
192
186
193
187
Identify aliases for recognized flags.
194
188
195
189
Automatically executes `recognize` for any flags specified amongst the defaults.
196
190
197
-
### allowMultipleValues('flag1', 'flag2', ...)
191
+
####allowMultipleValues('flag1', 'flag2', ...)
198
192
199
193
By default, if the same flag is defined multiple times, only the last value is recognized. Setting `allowMultiple` on a flag will capture all values (as an array).
200
194
201
195
Automatically executes `recognize` for any flags specified amongst the defaults.
202
196
203
-
### setOptions('flag', 'optionA', 'optionB')
197
+
####setOptions('flag', 'optionA', 'optionB')
204
198
205
199
A list/enumeration of values will be enforced _if_ the flag is set. If a flag contains a value not present in the list, a violation will be recognized.
206
200
@@ -212,49 +206,71 @@ Automatically executes `recognize` for any flags specified amongst the defaults.
212
206
213
207
Enforcement methods are designed to help toggle rules on/off as needed.
214
208
215
-
There is no special method to enforce a flag value to be within a list of valid options, because this is enforced automatically.
209
+
There is no special method to enforce a flag value to be within a list of valid options (enumerability), _because this is enforced automatically_.
216
210
217
-
### disallowUnrecognized()
211
+
####disallowUnrecognized()
218
212
219
213
Sets a rule to prevent the presence of any unrecognized flags.
220
214
221
-
### allowUnrecognized()
215
+
####allowUnrecognized()
222
216
223
217
Sets a rule to allow the presence of unrecognized flags (this is the default behavior).
224
218
225
-
### ignoreDataTypes()
219
+
####ignoreDataTypes()
226
220
227
221
This will ignore data type checks, even if the `types` method has been used to enforce data types.
228
222
229
-
### enforceDataTypes()
223
+
####enforceDataTypes()
230
224
231
225
This will enforce data type checks. This is the default behavior.
232
226
227
+
---
228
+
233
229
## Helper Methods
234
230
235
231
The following helper methods are made available for developers who need quick access to flags and enforcement functionality.
236
232
237
-
### enforceRules()
233
+
####enforceRules()
238
234
239
235
This method can be used within a process to validate flags and exit with error when validation fails.
240
236
241
-
### value(flagname)
237
+
####value(flagname)
242
238
243
239
Retrieve the value of a flag. This accepts flags or aliases. If the specified flag does not exist, a value of `undefined` is returned.
244
240
245
-
### exists(flagname)
241
+
####exists(flagname)
246
242
247
243
Returns a boolean value indicating the flag exists.
248
244
249
245
---
250
-
## Metadata
246
+
## Defining Metadata
251
247
252
248
The following methods are made available to manage metadata about flags.
253
249
254
-
### describe(flagname, description)
250
+
####describe(flagname, description)
255
251
256
-
Use this message to store a description of the flag. This is stored as metadata. This will throw an error if the flag does not exist.
252
+
Use this message to store a description of the flag. This will throw an error if the flag does not exist.
257
253
258
-
### description(flagname)
254
+
####description(flagname)
259
255
260
256
Retrieve the description of a flag. Returns `null` if no description is found.
257
+
258
+
---
259
+
260
+
## Parser Properties
261
+
262
+
These are readable properties of the parser. For example:
263
+
264
+
```javascript
265
+
importArgsfrom'@author.io/arg'
266
+
267
+
Args.configure({...})
268
+
269
+
console.log(Args.flags, Args.data, ...)
270
+
```
271
+
272
+
-`flags`: An array of the unique flag names passed to the application.
273
+
-`data`: A key/value object representing all data passed to the application. If a flag is passed in more than once and duplicates are _not_ suppressed, the value will be an array.
274
+
-`length` The total number of arguments passed to the application.
275
+
-`valid` A boolean representing whether all of the validation rules passed or not.
276
+
-`violations` An array of violations (this is an empty array when everything is valid).
0 commit comments