An easy tool for generating CLI and controling command params
version: 0.0.6
Date: 2019/1/17
Support parsing command line arguments and outputting doc quickly
- easy api, fast CLI initialization
- flag module could parse command line arguments and call registered function
- output module could output formatted helpInfo doc。
- absolute seperation among modules although you can inject output instance to flag instance to generate helpInfo about command or option conveniently
- similar api to commander ,nearly all api support chain call(flag's run method and output's render method not support)
├─example // demo
|─src
│ └─core // source code
└─test // test
yarn add @alanchenchen/commandlineflag
ornpm install --save @alanchenchen/commandlineflag
- npm package export an object including two methods:
flag
resolve command line argumentsoutput
output formatted helpInfo doc
method returns a Flag instance
-
param
basic function that parse cmd arguments, returns a Flag instance。Function arguments:- opts
[Object]
- index
[Number]
cmd argument index,default is undefined - flag
[String]
cmd argument flag name,default is undefined - action
[Function]
callback function while matching cmd argument successfully- first argmuent is an object inculding two keys,index and param
- second argument is an array that show a list including all cmd arguments
- callback function trigger rule:
- only index, if cmd argument of index exsit, trigger
- only flag, if cmd argument of flag exsit, trigger. flag should not match the index.
- both index and flag, should match cmd argument of index and cmd argument of flag, trigger
- index
- opts
-
command
parse command (index is 0),returns a Flag instance。Function arguments:- flag
[String]
command name,required - desc
[String]
command helpInfo - action
[Function]
callback function while matching cmd argument successfully
if there are two arguments,the second one must be action,if three,the second one must be desc,the third one must be action。
- flag
-
option
parse option (only flag),returns a Flag instance。Function arguments:- flag
[String]
option name,required - desc
[String]
option helpInfo - action
[Function]
callback function while matching cmd argument successfully
if there are two arguments,the second one must be action,if three,the second one must be desc,the third one must be action。
- flag
-
version
parse version option,returns a Flag instance and output version message。Function arguments:- desc
[String]
version message - flag
[String]
version flag,default is '-V | --version', you could rewrite it
- desc
-
register
register prototype method globally,returns a Flag instance。command、option and version method are all implemented by register。Function arguments:- name
[String]
method name,required - handler
[Function]
trigger function while you use the register method, usually use param method to implenment it。Such as :const program = new Flag() program.register('help', info => { // if you want help method to be chain called,return program.param() or program return program.param({ flag: '--help', action() { console.log(info) } }) }) // now,any Flag instance could include help method program.help('help info')
- name
-
inject
inject Output instance,would output doc while running,returns a Flag instance。Function arguments:- OutputInstancen
[Object]
Output instance which would be injectd,required
- OutputInstancen
-
run
run the Flag instance, no return value。
must call run method otherwise the cmd arguments would not be parsed
method returns a Output instance。
writeUsage
output usage info,returns a Output instance。Function arguments:- usageInfo
[String]
main usage info - description
[String]
usage description bellow usageInfo,optional
- usageInfo
writeCommands
output command helpInfo,returns a Output instance。Function arguments:- if there is only one argument:
- info
[Array]
array item is an object including title(command name) and desc(command description)
- info
- if there are two arguments:
- commandInfo
[String]
command name - description
[String]
command description
- commandInfo
- if there is only one argument:
writeOptions
output option helpInfo,returns a Output instance。Function arguments:- if there is only one argument:
- info
[Array]
array item is an object including title(command name) and desc(command description)
- info
- if there are two arguments:
- optionInfo
[String]
option name - description
[String]
option description
- optionInfo
- if there is only one argument:
write
info at the end of doc,returns a Output instance。Function arguments:- desc
[String]
customed info ,support multiple arguments
- desc
render
generate helpInfo doc,returns a string of doc。Function arguments:- isShowLog
[Boolean]
whether output doc to stdout,default is true
- isShowLog
must call render method otherwise the doc would not be generated. If you inject Output instance to Flag instance, render method is not necessary since Flag instance implement render method
see hotload-cli
- there is only one test about
command()
、option()
andversion()
ofFlag
instance - the test framework is mocha,if you want to add unit tests,follow :
git clone git@github.com:LionOcean/CommandLineFlag.git
- add js which should have
*.test.js
extname yarn
ornpm install
install devdependences mochanpm test
open terminal to see result
- MIT