arg-to-object
v1.2.3
Published
dynamically construct a command line parser based on the object provided to it
Downloads
7
Maintainers
Readme
ARG-TO-OBJECT
ATO is a quick-and-dirty argument parser meant for situations when a complicated CLI isn't necessary.
Use process.argv
by default.
const ato = require('arg-to-object')
var params = ato.parse()
An object can be used for defaults.
// node sample.js -files a b c -r false
var defaults = { files: [], recursive: true }
var params = ato.parse(defaults)
/*
* {
* files: ["a", "b", "c"],
* recursive: false
* }
*/
Argument shorthands are expanded by a simple prefix-search
// node sample.js -f a b c -r
params = ato.parse(defaults)
/* '-f' extends to 'files'
* { files: ["a", "b", "c"], ... }
*/
Pass in arguments manually.
ato.parse(process.argv, defaults)
Without a default object, the parsed object will constructed with _-tak_s as keys.
The arguments -f a b c -r
will yield
{
f: ["a", "b", "c"],
r: true
}
Type interpolation examples.
args=-r
result={r: true}
args=-r 1 2 3
result={r: [1, 2, 3]}
args=-r false
result={r: false}
args=-r -f hello
result={r: true, f: 'hello'}