Git Product home page Git Product logo

command-line-arguments-parser's Introduction

command-line-arguments-parser

CircleCI Join the chat at https://gitter.im/command-line-arguments-parser/Lobby License npm

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=require('command-line-arguments-parser');

let parserName=new Parser();

//the following are methods to specify rules

//to set default option for your command
parserName.setDefaultOption(option);
//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 hyphen
parserName.addLegalOption(option1 ,value1ValidatorCallback);

//if your options doing same work you need to add replaces
parserName.addReplacer(key ,value); //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 LongName
parserName.addLegalLongName('--'+yourLongNameName);


//to enable combined flags
parserName.enableCombinedFlags(); // 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 time
parserName.setMaximumOptions(number);
/* 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.parse(args); //example let args =['-n','10','files'] for head

Examples

Head command Parser
  let Parser=require('command-line-arguments-parser');

  let headParser = new Parser();
  //default option 'n'
  headParser.setDefaultOption('n');
  //two c and n options which take only number
  headParser.addLegalOption('-n',isItNumber);
  headParser.addLegalOption('-c',isItNumber);

  // -- and -n10 functionality are same
  headParser.addReplacer('--','-n10');

  // --help which don't need any input
  headParser.addLegalLongName('--help');

  //by default combined flags are false
  headParser.enableCombinedFlags();

  //maximum options set to 1
  headParser.setMaximumOptions(1);

  let isItNumber = function (value) {
    return (+value>0);
  };

Different cases output for above parser rules

  let args=['--help'];
  //output
  { arguments: [],
  LongNames: [ '--help' ],
  optionSetBy: 'default',
  flags: {},
  defaultOption: 'n' }

  args=['-n10','-c12'];
  //output
  Error:Maximum options reached

  args=['-n10',"toDo.txt"];
  //output
  {
    arguments:['toDo.txt'],
    LongNames:[],
    optionSetBy:'default',
    flags:{n:10},
    defaultOption:'n'
  }

  args=['-n12',"toDo.txt",'--help'];//once it see argument remaining everything is argument
  //output
  {
    arguments:['toDo.txt','--help'],
    LongNames:[],
    optionSetBy:'default',
    flags:{n:12},
    defaultOption:'n'
  }

  args=[];
  //output
  {
    arguments:[],
    LongNames:[],
    optionSetBy:'default',
    flags:{},
    defaultOption:'n'
  }

  args =['-10'];
  //output
  {
    arguments:[],
    LongNames:[],
    optionSetBy:'n',
    flags:{n:10},
    defaultOption:'n'
  }

Unsupported Cases

//when args as below
args=['-n10','-20'];
//it will replace n value with 20 because default option is n
{
  arguments:[],
  LongNames:[],
  optionSetBy:'n',
  flags:{n:20},
  defaultOption:'n'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.