1.0.1 • Published 5 years ago
simple-args-parser v1.0.1
simple-args-parser
Parse command line arguments (process.argv[]) in one function
Syntax:
args = parser.parse(process.argv, config, fallback?)Config:
{
short: [],
long: [],
stupid: [],
errOnDisallowed: true
}short[] is an array of short arguments (eg. -a)
long[] is an array of long arguments (eg. --long)
stupid[] is an array of stupid arguments (Long arguments with one -, eg -stupid)
If you want to get the argument parameter (--argument 'parameter'), add a : after the argument in the array
// Without argument
short: ['a']
// With argument
short: ['a:']As you may have noticed, dont put the - with the argument into the array.
Fallback:
Fallback is a function called when an error is recieved, called by fallback(err). If you dont specify a custon one, the error will be thrown upon encounter.
Example:
const parser = require('simple-args-parser')
args = parser.parse(process.argv, {
short: ['a:', 'b:', 'c'],
long: ['hello', 'world:'],
stupid: ['powershell', 'bill:'],
errOnDisallowed: true
}, (err) => {
console.log(err)
})
console.log(args)Try running this script like node test.js -a word -b 'this is a string' -c --hello --world indeed -powershell -bill 'gates'. It should output
{
a: 'word',
b: 'this is a string',
c: true,
hello: true,
world: 'indeed',
powershell: true,
bill: 'gates'
}