command-line-arguments-parser
This is a command line arguments parser written in Javascript that helps parse command line arguments and it will return object with boolean flags ,value flags, arguments, optionSetBy and default option. You should define rules to parse. I am providing different methods to specify rules
Motivation
I am implementing head (shell command) functionality using node js i found that it's difficult write individual parser for each command So by installing this module you can build any command easily even if you are building your own command. My Mentor key-val parser motivated me a lot.
Installation
npm install command-line-arguments-parser
Usage
let Parser=; let parserName=; //the following are methods to specify rules //to set default option for your commandparserName;//to add options which are valid you need to pass value validator callback to validate value given to your option in this your should start with hyphenparserName; //if your options doing same work you need to add replacesparserName; //example ('-h','--help') //if your option don't need value you need to add into LongNames//it should start with double hyphen to make valid LongNameparserName; //to enable combined flagsparserName; // example -abc 20 30 40 it will parse as -a20,-b30,-c40 //It helps to specify how many maximum options you can parse at a timeparserName;/* parser.setMaximumOptions(1);your arguments -n 10 -c 20 it will throw error maximum options reached */ //pass an array of things to parse method to get parsedArguments parserName; //example let args =['-n','10','files'] for head
Examples
Head command Parser
let Parser=; let headParser = ; //default option 'n' headParser; //two c and n options which take only number headParser; headParser; // -- and -n10 functionality are same headParser; // --help which don't need any input headParser; //by default combined flags are false headParser; //maximum options set to 1 headParser; let { return +value>0; };
Different cases output for above parser rules
let args='--help';//outputarguments:LongNames: '--help'optionSetBy: 'default'flags: {}defaultOption: 'n'args='-n10''-c12';//outputError:Maximum options reachedargs='-n10'"toDo.txt";//outputarguments:'toDo.txt'LongNames:optionSetBy:'default'flags:n:10defaultOption:'n'args='-n12'"toDo.txt"'--help';//once it see argument remaining everything is argument//outputarguments:'toDo.txt''--help'LongNames:optionSetBy:'default'flags:n:12defaultOption:'n'args=;//outputarguments:LongNames:optionSetBy:'default'flags:{}defaultOption:'n'args ='-10';//outputarguments:LongNames:optionSetBy:'n'flags:n:10defaultOption:'n'
Unsupported Cases
//when args as belowargs='-n10''-20';//it will replace n value with 20 because default option is n arguments: LongNames: optionSetBy:'n' flags:n:20 defaultOption:'n'