flag-parse
v0.1.0
Published
a node tool for parse command flag
Downloads
39
Readme
Flag-parse
A node tool for parse command flag
function
we provide a tool to parse the command flag in node env, if the field has double horizontal line --name=roadhub
, it is a full name ,else if the field has a single horizontal line only,it is a shortcut name -n=roadhub
,it 's value will be map to it's full name's value
> node -f --name=roadhub --age=12 index.js
in index.js
var flag = require("flag-parse").default
/**
* default: a default value when process.argv has no this flag
* type: is data type for this field , one of `string` , `number` and `bool`
* usage: is usage for this field ,it will be print when you call flag.printHelp
* shortcut: this is optional field , it is a abbreviation for this field
*/
let flags = flag.parse({
name:{default: "", type: "string", usage:"user's name",shortcut:"n"},
fork:{default: true, type: "bool", usage:"is fork",shortcut:"f"},
})
flag.printHelp("usage: `git <command>`, follow flag is useable:", "thank you to use git")
you can get this argument in flags, if you forget to config a field , it will be push to rest
by the original order
console.log(flags) // {fork: true, name: "roadhub",}
console.log(flag.rest) //["--age=12", "index.js"]
flag.printHelp("usage: `git <command> <flag>`, follow flag is useable:", "thank you to use git")
/**
* usage: `git <command> <flag>`, follow flag is useable:
*
* -name,-n user's name
* -fork,-f is fork
*
* thank you to use git
*/
thanks