Socket
Book a DemoInstallSign in
Socket

@fav/cli.parse-argv

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fav/cli.parse-argv

Parses command line arguments.

0.2.0
latest
Source
npmnpm
Version published
Weekly downloads
25
177.78%
Maintainers
1
Weekly downloads
 
Created
Source

@fav/cli.parse-argv NPM MIT License Build Status Build Status Coverage status

Parses command line arguments.

"fav" is an abbreviation of "favorite" and also the acronym of "for all versions". This package is intended to support all Node.js versions as possible. At least, this package supports Node.js >= v0.10.

Install

To install from npm:

$ npm install --save @fav/cli.parse-argv

NOTE: npm < 2.7.0 does not support scoped package, but old version Node.js supports it. So when you use such older npm, you should download this package from github.com, and move it in node_modules/@fav/cli.parse-argv/ directory manually.

Usage

$ node sample.js --abc-def --ghi 123 -j=45 k 6
(sample.js)
var parseArgv = require('@fav/cli.parse-argv');

parseArgv();
// => { options: { 'abc-def': true, abcDef: true, ghi: 123, j: 45 }, args: ['k', 6] }

parseArgv(process.argv.slice(2));
// => { options: { 'abc-def': true, abcDef: true, ghi: 123, j: 45 }, args: ['k', 6] }

API

.parse([ argv ] [, configs ]) : object

Parses command line arguments and returns a result object which has groups of options and normal arguments.

Parameters:

ParameterTypeDescription
argvArrayAn argument array, or an array same with process.argv.slice(2) if not be specified. (Optional)
configsobjectAn object which configure parsing. (Optional)

Returns:

A parsed result object, of which keys are as follows:

  • args [Array] : is an array of normal arguments.
  • options [object] : is an object which has options represented by key-value pairs.

Type: object

Short options and long options

There are two types for options: short option and long option. A short option is single character following single hyphen (like -c). A long option is basically a kebab case string following double hyphens (like --sss-sss). An argument which doesn't start with hyphens is operated as a normal argument.

Both options can be followed by an equal mark and a value string (like -c=vvv or --sss=vvv). In this case, the option string is operated as an equation.

Short options can be concatenated to an option string (like -abc instead of -a -b -c). If such option string is followed by an equal mark and a value string, the value is set to the last short option (e.g. -abc=1 is parsed into a=true, b=true, c=1).

If a long option can be converted to a camel case string which is different from an original option string, this function sets both an original option and a camel case option (e.g. --abc-def is parsed into 'abc-def'=true, abcDef=true).

Parsing with no config

If a parameter configs is not specified, this function parses command line arguments in default way. In default, all control-codes, marks, and numbers in ASCII characters are ignored as short options.

The normal argument which follows a option is always operated as a value of the option. Even if an argument starts with hyphens, but if it follows an option and can be converted to a number, it is operated as a value of the option.

The formats of short options are as follows:

FormatValue typeResult
-ctrue{ options: { c: true, ...}, args: [...] }
-cnnnnumber{ options: { c: nnn, ...}, args: [...] }
-c=nnnnumber{ options: { c: nnn, ...}, args: [...] }
-c=sssstring{ options: { c: 'sss', ...}, args: [...] }
-c nnnnumber{ options: { c: nnn, ...}, args: [...] }
-c -nnnnumber{ options: { c: -nnn, ...}, args: [...] }
-c sssstring{ options: { c: 'sss', ...}, args: [...] }

The formats of long options are as follows:

FormatValue typeResult
--sss-ssstrue{ options: { 'sss-sss': true, sssSss: true, ...}, args: [...] }
--no-sss-sssfalse{ options: { 'sss-sss': false, sssSss: false, ...}, args: [...] }
--sss-sss=nnnnumber{ options: { 'sss-sss': nnn, sssSss: nnn, ...}, args: [...] }
--sss-sss=sssstring{ options: { 'sss-sss': 'sss', sssSss: 'sss', ...}, args: [...] }
--sss-sss nnnnumber{ options: { 'sss-sss': nnn, sssSss: nnn, ...}, args: [...] }
--sss-sss -nnnnumber{ options: { 'sss-sss': -nnn, sssSss: -nnn, ...}, args: [...] }
--sss-sss sssstring{ options: { 'sss-sss': 'sss', sssSss: 'sss', ...}, args: [...] }

Parsing with configs

If a parameter configs is specified, this function parses command line arguments according with a configuration in configs corresponding to each specified option.

Even if an option is followed by a normal argument but if the .type property of the configuration for the option, the value of the option is true. On the other hand, if an option is not followed by a normal argument (and is not equation format), and if .type property of a configuraton is string or number, then the value of the option is '' or NaN.

When a value of an option cannot be converted to the specified type or specified choices in configs, this function throws an error. See Error section.

List of configurations

This function can parse with a configuration object which can have the following options. These options are similar with yargs's options, but the behaiors are not entirely same with it.

  • alias [string | Array] : sets alias option name(s).
  • choices [any | Array] : limits valid values of the option.
  • coerce [function] : provides a synchronous function to coerce or transform the value(s) of the option.
  • default [any] : sets a default value of the option if the option was not specified.
  • demandOption [boolean] : demands that the option is given.
  • requiresArg [boolean] : requires that the option is specified with a value.
  • array [boolean] : if true, the option has its value as an array.
  • type [string] : specifys the type of the option from following choices:
    • 'boolean' : interprets the option as a boolean flag.
    • 'count' : interprets the option as a count of boolean flags.
    • 'number' : interprets the option as a number.
    • 'string' : interprets the option as a string.

choices and requiresArg is available only when type is 'number' and 'string'.

Error

If this function failed to parse, it throws an error which has properties: .option and .reason.

  • option [string] : is the option name.
  • reason [string] : is the reason of the parsing error.
    • 'noRequiredArg' : If the option has no value though config.requiresArg is true.
    • 'notInChoices' : If the option is not in config.choices.
    • 'duplicatedNameOrAlias' : If the option name or alias is already used.
    • 'noDemandedOption' : If the option is not specified though config.demandOption is true.

Checked

Node.js (4〜9)

Platform456789
macOS
Windows10
Linux

io.js (1〜3)

Platform123
macOS
Windows10
Linux

Node.js (〜0.12)

Platform0.70.80.90.100.110.12
macOS
Windows10
Linux

License

Copyright (C) 2018-2019 Takayuki Sato

This program is free software under MIT License. See the file LICENSE in this distribution for more details.

Keywords

argv

FAQs

Package last updated on 05 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.