https://github.com/nrjais/cmd-line-args-parser
An parser to parse arguments given in terminal to make an terminal or command line app.
https://github.com/nrjais/cmd-line-args-parser
argument-parsing command-line command-line-arguments-parser command-line-parser parser-framework
Last synced: 4 months ago
JSON representation
An parser to parse arguments given in terminal to make an terminal or command line app.
- Host: GitHub
- URL: https://github.com/nrjais/cmd-line-args-parser
- Owner: nrjais
- License: apache-2.0
- Created: 2017-11-08T17:19:11.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-15T05:27:06.000Z (over 8 years ago)
- Last Synced: 2025-02-12T12:07:07.162Z (about 1 year ago)
- Topics: argument-parsing, command-line, command-line-arguments-parser, command-line-parser, parser-framework
- Language: JavaScript
- Size: 46.9 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://gitter.im/cmd-line-args-parser/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://circleci.com/gh/nrjais/cmd-line-args-parser/tree/master) [](https://travis-ci.org/nrjais/cmd-line-args-parser) [](https://opensource.org/licenses/Apache-2.0)
# cmd-line-args-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 array ,value-option object and arguments list. You have to define rules to parse in a defined structure.
## 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.
## Installation
`npm install cmd-line-args-parser`
## Usage
```javascript
var Parser=require('cmd-line-args-parser');
var parserName=new Parser(rules);
//the following is the structure for specifying rules
rules = {
validOptions: [], //all the valid options that require values
default: { //default option to set if no option specified
'n': 10
},
minimum: 1, /*
minimum number of required options if the arguments have
less than minimum options than default option will be set*/
maximum: 1, /*
maximum number of required options that are allowed
otherwise an error will be thrown*/
flags: [], //all the flags allowed
replacer: {} /*
rules that define replacers so that they can be converted
to a flag
*/
}
//pass an array of things to parse method to get parsedArguments
parserName.parse(args); //example let args =['-n','10','files'] for head
```
### Examples
#### Head command Parser
```javascript
var Parser=require('cmd-line-args-parser');
rules = { //rules for head command
validOptions: ['n','c'],
default: {
'n': 10
},
minimum: 1,
maximum: 1,
flags: ['h'],
replacer: {'help':'h'} //here --help will be replaced by 'h' so 'h' should be valid flag
}
parserName.setCombinedFlags(true);
/*
false by default
if false then two or more flags cannot be combined
like '-acs' will not work
if true then it will be ['a','c','s']
*/
//default if not given
let containsValue = function(value) {
let regex = /(\d)+$/g
return regex.test(value);
}
/*
containsValue is a function that can verify if a value is in the option or not
like -n10 here 10 is the value so the function can determine if the
option contains the value
or not.
*/
parserName.setContainsValue(containsValue);
let isNumber = function(value){
return Number.isInteger(+value);
};
/*
isNumber is the function that determines if the value given value is legal or not
if this function is not given then the below function is used by default.
*/
parserName.setIsValue(isNumber);
/*
if no isValue function is set then the above isNumber will be used
by default
*/
let headParser = new Parser(rules);
/*
once some arguments are parsed then you need to reset the parser
to parse new arguments again
you can do that by
*/
parserName.reset();
//then you can reuse your parser
parserName.parse(args);
```
## Errors thrown at different cases
```javascript
{
name:'MaxOptions',
message:'Cannot combine : -option1 -option2'
}
{
name:'MissingValue',
message:'Value of : -option is missing'}
{
name:'IllegalOption',
message:'Illegal option : -option'
}
{
name:'CombiningFlags', //only when combinedFlags is set false
message:'Combining Multiple flags not allowed'
}
```
## Different cases output for above parser rules
```javascript
args=['-n10','-c12'];
//output
Error:Cannot combine options : -n -c
args=['-n10',"toDo.txt"];
//output
{
flags: [],
options: {
n: 10
},
setByDefault : false, //this will be true when the option is set using default
arguments: ["toDo.txt"]
}
args=["--help"];
//output
{
flags: ['h'],
options: {
n: 10
},
setByDefault : true,
arguments: []
}
args=['-n12',"toDo.txt"];//once it see argument remaining everything is argument
//output
{
flags: [],
options: {
n: 12
},
setByDefault : false,
arguments: ["toDo.txt"]
}
args=[];
//output
{
flags: [],
options: {
n: 10
},
setByDefault : true,
arguments: []
}
args =['-10'];
//output
{
flags: [],
options: {
n: 10
},
setByDefault : false,
arguments: []
}
```
### Unsupported Cases
```javascript
//when args as below
args=['-n10','-20'];
//it will replace n value with 20 because default option is n
{
flags: [],
options: {
n: 20
},
setByDefault : false,
arguments: ["toDo.txt"]
}
```