argufy
yarn add -E argufy
argufy
will parse command line arguments to Node.js CLI programs.
API
The package assumes that the arguments begin from the 3rd position, i.e.,
node example.js --title "Hello World"
argufy(config, argv): object
The flags from the arguments will be extracted according to the configuration object and the arguments array. If arguments array is not passed, process.argv
is used to find arguments.
import argufy from 'argufy'
const config = {
title: { short: 't', command: true },
list: { short: 'l', boolean: true },
app: 'a',
delay: 'y',
file: 'f',
wait: { short: 'w', number: true },
'no-empty': 'e',
resize: 'z',
colors: 'c',
dir: 'D',
}
const res = argufy(config, process.argv)
console.log(res)
node example.js --title "Hello World" -w 10 -l -app Argufy
node example.js "Hello World" -w 10 -l -app Argufy
{ title: 'Hello World', list: true, app: 'Argufy', wait: 10 }
The configuration for each flag can either be a shorthand string, or an object. If it is an object, it can include the following parameters:
property | type | description | example |
---|
short | string | A short version of the argument. | program -t title |
boolean | boolean | Whether to parse as a boolean, does not require a value. | program -d |
number | boolean | Parse argument as a number. | program -n 10 |
command | boolean | Can this argument be a command, i.e., be the first argument without having to follow a flag. Sets the argument to be a string. | program command |
multiple | boolean | If command is true, should multiple words be parsed as an array of commands. Sets the argument to be an array of strings. | program command1 command2 |
(c) artdecocode 2018