ts-flag
the commandline parser for node.js, inspired by Go's Flag package
Installation
npm install ts-flag
USAGE
in .js
let flag = TSFlag;let tsFlag = ; try let ea = tsFlag; let love = tsFlag; let she = tsFlag; console; catch e console; tsFlag;
in .ts
; let flag = ; try let ea: number = flag; // error ea type is number but return type is string, and then parameter type error let ea2: number = flag; catch e console; flag;
;
Example
node .\example.js -ea=1 -love -girlfriend=Jane=> 1 true Jane node .\example.js -ea=1 -girlfriend=Jane=> 1 false Jane node .\example.js -ea -girlfriend=Jane=> 0 false Jane//ea set initial value node .\example.js -ea -girlfriend=> 0 false everyone
if not set optionName (except Boolean Type)
node .\example.js -ea=1Error: girlfriend option is not contained at stack history...Usage of this - ea<number>, initValue=1 [count of tickes] - love<boolean>, initValue=false [if you love me write this option] - girlfriend<string>, initValue=everyone [my girl friend]
Array Example
let flag = TSFlag;let tsFlag = ; try let Astudents = tsFlag; let Bstudents = tsFlag; let scores = tsFlag; console; catch e console; tsFlag;
node .\example.js -students="IronMan","Thor","Hulk" -Bstus="SuperMan","BatMan" -scores="90,84,67"=> [ 'IronMan', 'Thor', 'Hulk' ] [ 'SuperMan', 'BatMan' ] [ 90, 84, 67 ] node .\example.js -students="IronMan","Thor","Hulk" -Bstus -scores="90,84,67"=> [ 'IronMan', 'Thor', 'Hulk' ] [ 'john', 'mark' ] [ 90, 84, 67 ]
if you want the custom Arguments, inject a stringArray into the constructor
let flag = require('ts_flag').TSFlag;// let args = ['name=IronMan', 'Type=Mark1', 'power=99'];let args = ["-ea=5", "girlfriend=Jane"];let tsFlag = new flag(args); try { let ea = tsFlag.int('ea', 0, 'count of tickes'); let love = tsFlag.bool('love', false, 'if you love me write this option'); let she = tsFlag.str('girlfriend', 'everyone', 'my girl friend'); console.log(ea, love, she);} catch (e) { console.log(e); tsFlag.Usage()}
Test
npm run test
Log
1.0.4 - Fixed an issue where the exposure type was displayed as number in'str' and'strArr' (operation is normal, only problems at display)
1.0.3 - Change the value to search for index
1.0.2 - just modify readme
1.0.1 - initial publish