arg2
v0.1.0
Published
Command line arguments parser for cli applications
Downloads
2
Readme
arg2
Command line arguments parser for cli applications
Usage
Install arg2 as dependencies in your project
npm i arg2 --save
# or yarn add arg2
A cli application demo in index.js
const arg2 = require('arg2');
arg2
.option('author <name>', 'a required project owner')
.option('email [name]', 'a optional owner email')
.action((val, options) => {
console.log(options);
});
arg2.parse(process.argv);
// Test command:
// node index.js --author blues --email xxxxx
// Output:
// { author: 'blues', email: 'xxxxx' }
Examples
The examples below your can find in examples
folder.
multiple command with options
const arg2 = require('arg2');
// Test command:
// node examples/command create helo --author foo --email bar
// Output:
// helo
// { author: 'foo', email: 'bar' }
arg2
.command(
'create [projectName]',
'create a project in current workspace',
'hello-world'
)
.option('author <author>', 'a required project owner')
.option('email [email]', 'a optional project owner email')
.action((val, options) => {
console.log(val);
console.log(options);
});
// Test command:
// node examples/command test demo
// Output:
// demo
// {}
arg2.command('test <projectName>', 'test project').action((val, options) => {
console.log(val);
console.log(options);
});
// Test command:
// node examples/command build hello --env qa
// Output:
// hello
// { env: 'qa' }
arg2
.command('build <projectName>', 'build your project with provided name')
.option('env [env]', 'build your project with a env', 'production')
.action((val, options) => {
console.log(val);
console.log(options);
});
arg2.parse(process.argv);
only options without command
const arg2 = require('arg2');
// Test command:
// node examples/option.js --author blues --email xxxxx
// Output:
// undefined
// { author: 'blues', email: 'xxxxx' }
arg2
.option('author <name>', 'a required project owner')
.option('email [name]', 'a optional owner email')
.action((val, options) => {
console.log(val);
console.log(options);
});
arg2.parse(process.argv);