A minimal node js CLI maker.
Note: Version 2 has breaking changes so make sure to read below and make changes to your application accordingly
- modified the look of usage menu, made it cleaner
- now args are accessible within the
run
property ofaddCommand
usingthis.args
. Check below for example.
- Removed previous methods and added chaining of methods.
setFlags
can be chained withaddCommand
setFlags
takes in multiple flag property. check below for example
No external dependencies used apart from chalk.js for coloured outputs.
- Parses command line arguments like
- -flag=value
- -flag value
- Allows user to set commands
- Can have same named flags for different commands
- Generates usage details of the CLI application
- use NPM
bash npm i termparse
and you are ready to go
const Termparse = require("termparse");
//create a instance
const tp=new Termparse();
//1st command
tp.addCommand({
name:"cmd1",
usage:"this is command 1",
run:function(){
//adding functionality to cmd1
//this.getFlag(flagName) returns flag object
console.log(`accessing flags using getFlag`,this.getFlag("flag1"));
//way to access arguments
console.log(this.args)
}
}).setFlags({ //chaining setFlags with addCommand
name:"flag1",
usage:"this is flag 1 for command 1",
type:"string",
value:"hahaha"
},{
name:"flag2",
usage:"this is flag 2 for command 1"
//no type and value passed implies default: type:"boolean" and value:false
});
// 2nd command
tp.addCommand({
name:"cmd2",
usage:"this is command 2",
run:function(){
//another way to access flag object this.flags.<flag_name>
console.log(`another way to access flag`,this.flags.gas1);
//another way to access args (non flag type)
console.log(tp.args);
}
}).setFlags({
name:"gas1",
usage:"this is flag 1 (gas 1) for command 2"
type:"number",
value:2000
});
// accessing help/usage menu
tp.showHelp()
//get args from comamnd line
var args=process.argv.slice(2);
//pass arguments to termparse
tp.parse(args);
Adds commands to the CLI application. Takes command property object as input like so
{
"name": "name of command here",
"usage": "usage details of the command",
"run": "adds functionality to commad"
}
usage
takes the details of what the command does which is recommended to generate a auto-usage guide.run
takes function and the function is called when the command is used in terminal.
NOTE: do not pass fat arrow function or ()=>{} to run. Rather use function(){}.
Adds flags/options to a specific command of your CLI application. This function is used by chaining it to the addCommand({...})
function.
Takes in multiple flag property objects as shown in the very first example.
{
"name": "name of flag",
"type": "type of flag",
"value": "value of flag",
"usage": "usage details"
}
-
type
can be eitherboolean
/string
/number
. If no type is passed then default isboolean
. -
value
if flag type is boolean then it takes true/false, default being false in boolean. If flag type is string then it takes string as value, default being empty string.
Gets flag/option object of a specific command of your CLI application. Can be used within the run
property of the addCommand({...})
flag
takes name of the flag
returns flag property object
//content of flag property object
{
"type": "type of flag",
"value": "value of flag",
"usage": "usage of the flag",
"present": "whether flag is passed as arg or not"
}
using getFlag()
lets user use the flags value to do various functions.