{\ttfamily arg} is yet another command line option parser.\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3097}{}\doxysection{Installation}\label{md_sfcode_node_modules_arg_README_autotoc_md3097}
Use Yarn or NPM to install.


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ yarn add arg }

\end{DoxyCode}


or


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install arg }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3098}{}\doxysection{Usage}\label{md_sfcode_node_modules_arg_README_autotoc_md3098}
{\ttfamily arg()} takes either 1 or 2 arguments\+:


\begin{DoxyEnumerate}
\item Command line specification object (see below)
\item Parse options ({\itshape Optional}, defaults to {\ttfamily \{permissive\+: false, argv\+: process.\+argv.\+slice(2), stop\+At\+Positional\+: false\}})
\end{DoxyEnumerate}

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\textquotesingle{}t consumed by options (commonly referred to as \char`\"{}extra\char`\"{} parameters) are added to {\ttfamily result.\+\_\+}, which is {\itshape always} an array (even if no extra parameters are passed, in which case an empty array is returned).


\begin{DoxyCode}{0}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{// `options` is an optional parameter}
\DoxyCodeLine{const args = arg(spec, options = \{permissive: false, argv: process.argv.slice(2)\}); }

\end{DoxyCode}


For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ node ./hello.js -\/-\/verbose -\/vvv -\/-\/port=1234 -\/n 'My name' foo bar -\/-\/tag qux -\/-\/tag=qix -\/-\/ -\/-\/foobar }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{// hello.js}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{const args = arg(\{}
\DoxyCodeLine{    // Types}
\DoxyCodeLine{    '-\/-\/help':    Boolean,}
\DoxyCodeLine{    '-\/-\/version': Boolean,}
\DoxyCodeLine{    '-\/-\/verbose': arg.COUNT,   // Counts the number of times -\/-\/verbose is passed}
\DoxyCodeLine{    '-\/-\/port':    Number,      // -\/-\/port <number> or -\/-\/port=<number>}
\DoxyCodeLine{    '-\/-\/name':    String,      // -\/-\/name <string> or -\/-\/name=<string>}
\DoxyCodeLine{    '-\/-\/tag':     [String],    // -\/-\/tag <string> or -\/-\/tag=<string>}
\DoxyCodeLine{}
\DoxyCodeLine{    // Aliases}
\DoxyCodeLine{    '-\/v':        '-\/-\/verbose',}
\DoxyCodeLine{    '-\/n':        '-\/-\/name',    // -\/n <string>; result is stored in -\/-\/name}
\DoxyCodeLine{    '-\/-\/label':   '-\/-\/name'     // -\/-\/label <string> or -\/-\/label=<string>;}
\DoxyCodeLine{                              //     result is stored in -\/-\/name}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(args);}
\DoxyCodeLine{/*}
\DoxyCodeLine{\{}
\DoxyCodeLine{    \_: ["{}foo"{}, "{}bar"{}, "{}-\/-\/foobar"{}],}
\DoxyCodeLine{    '-\/-\/port': 1234,}
\DoxyCodeLine{    '-\/-\/verbose': 4,}
\DoxyCodeLine{    '-\/-\/name': "{}My name"{},}
\DoxyCodeLine{    '-\/-\/tag': ["{}qux"{}, "{}qix"{}]}
\DoxyCodeLine{\}}
\DoxyCodeLine{*/ }

\end{DoxyCode}


The values for each key=$>$value pair is either a type (function or \mbox{[}function\mbox{]}) or a string (indicating an alias).


\begin{DoxyItemize}
\item In the case of a function, the string value of the argument\textquotesingle{}s value is passed to it, and the return value is used as the ultimate value.
\item In the case of an array, the only element {\itshape 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.
\item In the case of a string, an alias is established. If a flag is passed that matches the {\itshape key}, then the {\itshape value} is substituted in its place.
\end{DoxyItemize}

Type functions are passed three arguments\+:


\begin{DoxyEnumerate}
\item The parameter value (always a string)
\item The parameter name (e.\+g. {\ttfamily -\/-\/label})
\item The previous value for the destination (useful for reduce-\/like operations or for supporting {\ttfamily -\/v} multiple times, etc.)
\end{DoxyEnumerate}

This means the built-\/in {\ttfamily String}, {\ttfamily Number}, and {\ttfamily Boolean} type constructors \char`\"{}just work\char`\"{} as type functions.

Note that {\ttfamily Boolean} and {\ttfamily \mbox{[}Boolean\mbox{]}} have special treatment -\/ an option argument is {\itshape not} consumed or passed, but instead {\ttfamily true} is returned. These options are called \char`\"{}flags\char`\"{}.

For custom handlers that wish to behave as flags, you may pass the function through {\ttfamily arg.\+flag()}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{const argv = ['-\/-\/foo', 'bar', '-\/ff', 'baz', '-\/-\/foo', '-\/-\/foo', 'qux', '-\/fff', 'qix'];}
\DoxyCodeLine{}
\DoxyCodeLine{function myHandler(value, argName, previousValue) \{}
\DoxyCodeLine{    /* `value` is always `true` */}
\DoxyCodeLine{    return 'na ' + (previousValue || 'batman!');}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{const args = arg(\{}
\DoxyCodeLine{    '-\/-\/foo': arg.flag(myHandler),}
\DoxyCodeLine{    '-\/f': '-\/-\/foo'}
\DoxyCodeLine{\}, \{}
\DoxyCodeLine{    argv}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(args);}
\DoxyCodeLine{/*}
\DoxyCodeLine{\{}
\DoxyCodeLine{    \_: ['bar', 'baz', 'qux', 'qix'],}
\DoxyCodeLine{    '-\/-\/foo': 'na na na na na na na na batman!'}
\DoxyCodeLine{\}}
\DoxyCodeLine{*/ }

\end{DoxyCode}


As well, {\ttfamily arg} supplies a helper argument handler called {\ttfamily arg.\+COUNT}, which equivalent to a {\ttfamily \mbox{[}Boolean\mbox{]}} argument\textquotesingle{}s {\ttfamily .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 {\ttfamily ssh}\textquotesingle{}s multiple levels of verbosity ({\ttfamily -\/vvvv} being the most verbose).


\begin{DoxyCode}{0}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{const argv = ['-\/AAAA', '-\/BBBB'];}
\DoxyCodeLine{}
\DoxyCodeLine{const args = arg(\{}
\DoxyCodeLine{    '-\/A': arg.COUNT,}
\DoxyCodeLine{    '-\/B': [Boolean]}
\DoxyCodeLine{\}, \{}
\DoxyCodeLine{    argv}
\DoxyCodeLine{\});}
\DoxyCodeLine{}
\DoxyCodeLine{console.log(args);}
\DoxyCodeLine{/*}
\DoxyCodeLine{\{}
\DoxyCodeLine{    \_: [],}
\DoxyCodeLine{    '-\/A': 4,}
\DoxyCodeLine{    '-\/B': [true, true, true, true]}
\DoxyCodeLine{\}}
\DoxyCodeLine{*/ }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3099}{}\doxysubsection{Options}\label{md_sfcode_node_modules_arg_README_autotoc_md3099}
If a second parameter is specified and is an object, it specifies parsing options to modify the behavior of {\ttfamily arg()}.\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3100}{}\doxysubsubsection{$<$tt$>$argv$<$/tt$>$}\label{md_sfcode_node_modules_arg_README_autotoc_md3100}
If you have already sliced or generated a number of raw arguments to be parsed (as opposed to letting {\ttfamily arg} slice them from {\ttfamily process.\+argv}) you may specify them in the {\ttfamily argv} option.

For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const args = arg(}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        '-\/-\/foo': String}
\DoxyCodeLine{    \}, \{}
\DoxyCodeLine{        argv: ['hello', '-\/-\/foo', 'world']}
\DoxyCodeLine{    \}}
\DoxyCodeLine{); }

\end{DoxyCode}


results in\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const args = \{}
\DoxyCodeLine{    \_: ['hello'],}
\DoxyCodeLine{    '-\/-\/foo': 'world'}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3101}{}\doxysubsubsection{$<$tt$>$permissive$<$/tt$>$}\label{md_sfcode_node_modules_arg_README_autotoc_md3101}
When {\ttfamily permissive} set to {\ttfamily true}, {\ttfamily arg} will push any unknown arguments onto the \char`\"{}extra\char`\"{} argument array ({\ttfamily result.\+\_\+}) instead of throwing an error about an unknown flag.

For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{const argv = ['-\/-\/foo', 'hello', '-\/-\/qux', 'qix', '-\/-\/bar', '12345', 'hello again'];}
\DoxyCodeLine{}
\DoxyCodeLine{const args = arg(}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        '-\/-\/foo': String,}
\DoxyCodeLine{        '-\/-\/bar': Number}
\DoxyCodeLine{    \}, \{}
\DoxyCodeLine{        argv,}
\DoxyCodeLine{        permissive: true}
\DoxyCodeLine{    \}}
\DoxyCodeLine{); }

\end{DoxyCode}


results in\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const args = \{}
\DoxyCodeLine{    \_:          ['-\/-\/qux', 'qix', 'hello again'],}
\DoxyCodeLine{    '-\/-\/foo':    'hello',}
\DoxyCodeLine{    '-\/-\/bar':    12345}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3102}{}\doxysubsubsection{$<$tt$>$stop\+At\+Positional$<$/tt$>$}\label{md_sfcode_node_modules_arg_README_autotoc_md3102}
When {\ttfamily stop\+At\+Positional} is set to {\ttfamily true}, {\ttfamily arg} will halt parsing at the first positional argument.

For example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const arg = require('arg');}
\DoxyCodeLine{}
\DoxyCodeLine{const argv = ['-\/-\/foo', 'hello', '-\/-\/bar'];}
\DoxyCodeLine{}
\DoxyCodeLine{const args = arg(}
\DoxyCodeLine{    \{}
\DoxyCodeLine{        '-\/-\/foo': Boolean,}
\DoxyCodeLine{        '-\/-\/bar': Boolean}
\DoxyCodeLine{    \}, \{}
\DoxyCodeLine{        argv,}
\DoxyCodeLine{        stopAtPositional: true}
\DoxyCodeLine{    \}}
\DoxyCodeLine{); }

\end{DoxyCode}


results in\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{const args = \{}
\DoxyCodeLine{    \_: ['hello', '-\/-\/bar'],}
\DoxyCodeLine{    '-\/-\/foo': true}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3103}{}\doxysubsection{Errors}\label{md_sfcode_node_modules_arg_README_autotoc_md3103}
Some errors that {\ttfamily arg} throws provide a {\ttfamily .code} property in order to aid in recovering from user error, or to differentiate between user error and developer error (bug).\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3104}{}\doxyparagraph{ARG\+\_\+\+UNKNOWN\+\_\+\+OPTION}\label{md_sfcode_node_modules_arg_README_autotoc_md3104}
If an unknown option (not defined in the spec object) is passed, an error with code {\ttfamily ARG\+\_\+\+UNKNOWN\+\_\+\+OPTION} will be thrown\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{// cli.js}
\DoxyCodeLine{try \{}
\DoxyCodeLine{  require('arg')(\{ '-\/-\/hi': String \});}
\DoxyCodeLine{\} catch (err) \{}
\DoxyCodeLine{  if (err.code === 'ARG\_UNKNOWN\_OPTION') \{}
\DoxyCodeLine{    console.log(err.message);}
\DoxyCodeLine{  \} else \{}
\DoxyCodeLine{    throw err;}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\} }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{node cli.js -\/-\/extraneous true}
\DoxyCodeLine{Unknown or unexpected option: -\/-\/extraneous }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_arg_README_autotoc_md3105}{}\doxysection{License}\label{md_sfcode_node_modules_arg_README_autotoc_md3105}
Copyright \copyright{} 2017-\/2019 by ZEIT, Inc. Released under the \mbox{\hyperlink{md_sfcode_node_modules_arg_LICENSE}{MIT License}}. 