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

command-line-arguments-parser

v4.1.8

Published

This package help to parse command line arguments to specific structure you can customize it for any command , command line arguments to options ,boolean flags ,remaining unspecified arguments

Downloads

108

Readme

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'
}