Another simple argument parser
Arg
arg
is yet another command line option parser.
Installation
Use Yarn or NPM to install.
- ---- --- ---
or
- --- ------- ---
Usage
arg()
takes either 1 or 2 arguments:
- Command line specification object (see below)
- Parse options (Optional, defaults to
{permissive: false, argv: process.argv.slice(2), stopAtPositional: false}
)
It returns an object with any values present on the command-line (missing options are thus missing from the resulting object). Arg performs no validation/requirement checking - we leave that up to the application.
All parameters that aren't consumed by options (commonly referred to as "extra" parameters)
are added to result._
, which is always an array (even if no extra parameters are passed,
in which case an empty array is returned).
----- --- - --------------- -- --------- -- -- -------- --------- ----- ---- - --------- ------- - ------------ ------ ----- ------------------------
For example:
- ---- ---------- --------- ---- ----------- -- --- ----- --- --- ----- --- --------- -- --------
-- -------- ----- --- - --------------- ----- ---- - ----- -- ----- --------- -------- ------------ -------- ------------ ---------- -- ------ --- ------ -- ----- --------- -- ------ --------- ------- -- ------ -------- -- --------------- --------- ------- -- ------ -------- -- --------------- -------- --------- -- ----- -------- -- -------------- -- ------- ----- ------------ ----- --------- -- -- --------- ------ -- ------ -- ------ ---------- -------- -- ------- -------- -- ----------------- -- ------ -- ------ -- ------ --- ------------------ -- - -- ------- ------ ------------ --------- ----- ------------ -- --------- --- ------ -------- ------- ------ - --
The values for each key=>value pair is either a type (function or [function]) or a string (indicating an alias).
In the case of a function, the string value of the argument's value is passed to it, and the return value is used as the ultimate value.
In the case of an array, the only element must be a type function. Array types indicate that the argument may be passed multiple times, and as such the resulting value in the returned object is an array with all of the values that were passed using the specified flag.
In the case of a string, an alias is established. If a flag is passed that matches the key, then the value is substituted in its place.
Type functions are passed three arguments:
- The parameter value (always a string)
- The parameter name (e.g.
--label
) - The previous value for the destination (useful for reduce-like operations or for supporting
-v
multiple times, etc.)
This means the built-in String
, Number
, and Boolean
type constructors "just work" as type functions.
Note that Boolean
and [Boolean]
have special treatment - an option argument is not consumed or passed, but instead true
is
returned. These options are called "flags".
For custom handlers that wish to behave as flags, you may pass the function through arg.flag()
:
----- --- - --------------- ----- ---- - --------- ------ ------ ------ -------- -------- ------ ------- ------- -------- ---------------- -------- -------------- - -- ------- -- ------ ------ -- ------ --- - - -------------- -- ----------- - ----- ---- - ----- -------- -------------------- ----- ------- -- - ---- --- ------------------ -- - -- ------- ------ ------ ------- -------- --- -- -- -- -- -- -- -- -------- - --
As well, arg
supplies a helper argument handler called arg.COUNT
, which equivalent to a [Boolean]
argument's .length
property - effectively counting the number of times the boolean flag, denoted by the key, is passed on the command line..
For example, this is how you could implement ssh
's multiple levels of verbosity (-vvvv
being the most verbose).
----- --- - --------------- ----- ---- - --------- --------- ----- ---- - ----- ----- ---------- ----- --------- -- - ---- --- ------------------ -- - -- --- ----- -- ----- ------ ----- ----- ----- - --
Options
If a second parameter is specified and is an object, it specifies parsing options to modify the behavior of arg()
.
argv
If you have already sliced or generated a number of raw arguments to be parsed (as opposed to letting arg
slice them from process.argv
) you may specify them in the argv
option.
For example:
----- ---- - ---- - -------- ------ -- - ----- --------- -------- -------- - --
results in:
----- ---- - - -- ---------- -------- ------- --
permissive
When permissive
set to true
, arg
will push any unknown arguments
onto the "extra" argument array (result._
) instead of throwing an error about
an unknown flag.
For example:
----- --- - --------------- ----- ---- - --------- -------- -------- ------ -------- -------- ------ -------- ----- ---- - ---- - -------- ------- -------- ------ -- - ----- ----------- ---- - --
results in:
----- ---- - - -- --------- ------ ------ -------- -------- -------- -------- ----- -
stopAtPositional
When stopAtPositional
is set to true
, arg
will halt parsing at the first
positional argument.
For example:
----- --- - --------------- ----- ---- - --------- -------- --------- ----- ---- - ---- - -------- -------- -------- ------- -- - ----- ----------------- ---- - --
results in:
----- ---- - - -- --------- --------- -------- ---- --
Errors
Some errors that arg
throws provide a .code
property in order to aid in recovering from user error, or to
differentiate between user error and developer error (bug).
ARG_UNKNOWN_OPTION
If an unknown option (not defined in the spec object) is passed, an error with code ARG_UNKNOWN_OPTION
will be thrown:
-- ------ --- - ---------------- ------- ------ --- - ----- ----- - -- --------- --- --------------------- - ------------------------- - ---- - ----- ---- - -
---- ------ ------------ ---- ------- -- ---------- ------- ------------
License
Copyright © 2017-2019 by ZEIT, Inc. Released under the MIT License.
Repository
zeit/arg
来源:JavaScript中文网 ,转载请注明来源 本文地址:https://www.javascriptcn.com/post/50648