npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cmd-line-args-parser

v4.5.0

Published

This package can be used to parse command line arguments to a specific structure of rules you define that is used during parsing

Downloads

20

Readme

Join the chat at https://gitter.im/cmd-line-args-parser/Lobby CircleCI Build Status License

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

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

  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

{
  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

  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

//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"]
}