ts-flag
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

Build Status Coverage Status

ts-flag

the commandline parser for node.js, inspired by Go's Flag package

Installation

npm install ts-flag

USAGE

in .js

let flag = require('ts-flag').TSFlag;
let tsFlag = new flag();
 
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();
}

in .ts

import { TSFlag } from 'ts-flag';
 
let flag = new TSFlag();
 
try {
  let ea: number = flag.str('ea', 0, 'count of tickes'); // error ea type is number but return type is string, and then parameter type error
  let ea2: number = flag.int('ea', 0, 'correct type');
} catch (e) {
  console.log(e);
  flag.Usage();
}

errorPng;

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=1
Error: 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 = require('ts-flag').TSFlag;
let tsFlag = new flag();
 
try {
  let Astudents = tsFlag.strArr('students', [], 'students of class');
  let Bstudents = tsFlag.strArr('Bstus', ['john', 'mark']);
  let scores = tsFlag.intArr('scores', [], 'scores of students');
  console.log(Astudents, BAstudents, scores);
} catch (e) {
  console.log(e);
  tsFlag.Usage();
}
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

Package Sidebar

Install

npm i ts-flag

Weekly Downloads

1

Version

1.0.5

License

MIT

Unpacked Size

30.2 kB

Total Files

13

Last publish

Collaborators

  • sungeunsong