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

@open-tech-world/es-cli-args

v0.1.2

Published

A strict CLI arguments parser.

Downloads

2

Readme

@open-tech-world/es-cli-args

Build CodeFactor

A strict CLI arguments parser.

Features

Installation

Using npm

npm install @open-tech-world/es-cli-args

Using Yarn

yarn add @open-tech-world/es-cli-args

Usage

import { parser } from '@open-tech-world/es-cli-args';

parser(args: string | string[]): IOutObj;

Examples

Input:

parser('-a');

Output:

{ "args": [], "opts": { "a": true } }

Input:

parser('rm -rf a.txt');

Output:

{
  "args": ["rm", "a.txt"],
  "opts": { "r": true, "f": true }
}

Input:

parser('npm publish --dry-run');

Output:

{
  "args": ["npm", "publish"],
  "opts": { "dryRun": true }
}

Input:

parser('git commit -a -m="New commit msg"'));

Output:

{
  "args": ["git", "commit"],
  "opts": { "a": true, "m": "New commit msg" }
}

Input:

parser('git merge --no-commit');

Output:

{
  "args": ["git", "merge"],
  "opts": { "commit": false }
}

Input:

$ node example.js -x=1 -x=2

Output:

{
  "args": [],
  "opts": { "x": [1, 2] }
}

Input:

$ node example.js -y=1,2

Output:

{
  "args": [],
  "opts": { "y": [1, 2] }
}

Input:

parser('git config --system [email protected]');

Output:

{
  "args": ["git", "config", { "user": { "email": "[email protected]" } }],
  "opts": { "system": true }
}

Input:

parser('git config --global --alias.ci=commit'));

Output:

{
  "args": ["git", "config"],
  "opts": { "global": true, "alias": { "ci": "commit" } }
}

Parser Rules

Terminology:

The arguments may contain Commands, Positional arguments and Options.

The Options can be classified into Short options and Long options.

Commands:

A command represents an action name. It can have sub-commands.

Eg: git commit npm install

In the above example commit & install are commands for the git & npm respectively.

Positional arguments:

The positional arguments are parameters to a command. The order is often important.

Eg: cp SOURCE DEST

In the above example, the SOURCE & DEST are positional arguments for cp

Options:

The Options or Flags are named parameters.

Denoted with either a hyphen - and a single-letter name or a double hyphen -- and a multiple-letter name.

Eg:

Short option: rm -r file.ext

Long option: rm --recursive file.ext

Input:

The parser accepts both String and Array of Strings as input.

Eg:

'rm -rf photo.jpg'

['tar', '-tvf', 'archive.tar']

Or probably from process.argv.slice(2).

Parsing:

  • The arguments are separated by space character.

  • There is no additional configuration to specify arguments type.

  • The arguments types are auto inferred.

  • The commands & sub-commands are included in the args array property of the output object.

  • The positional arguments are also included in the args array property of the output object.

  • The options are default converted into camelCase and included in the opts object of output object.

    $ npm publish --dry-run
    { "args": ["npm", "publish"], "opts": { "dryRun": true } }
  • The default value for an option is boolean true.

  • A value can be set for options using = character.

    $ git commit -a -m='New commit msg'
    {
      "args": ["git", "commit"],
      "opts": { "a": true, "m": "New commit msg" }
    }
    $ ssh example.com -p=3322
    {
      "args": ["ssh", "example.com"],
      "opts": { "p": 3322 }
    }
    $ tar -cf archive.tar foo bar --level=5
    {
      "args": ["tar", "archive.tar", "foo", "bar"],
      "opts": { "c": true, "f": true, "level": 5 }
    }
  • The Options can contain Array values, use the ,(comma) character without space to create an array of values.

    $ node example.js -x=1,2
    { "args": [], "opts": { "x": [1, 2] } }
  • The short options can grouped.

    $ tar -tvf archive.tar
    {
      "args": ["tar", "archive.tar"],
      "opts": { "t": true, "v": true, "f": true }
    }
  • The grouped short options cannot be assigned a value.

    The following does not work

    $ git commit -am="Commit msg"
  • Boolean Negation: The options can be boolean negated using --no- prefix.

    $ git commit --no-verify
    { "args": ["git", "commit"], "opts": { "verify": false } }
  • Dot-Notation: The value of long options that contain .(Dot) character are converted into object.

    $ node example.js [email protected]
    { "args": [], "opts": { "user": { "email": "[email protected]" } } }
  • Output

    The parser returns an object containing all Commands & Positional arguments in an args array prop and all short & long options set in an object opts prop.

    {
      "args": [...],
      "opts": {...}
    }

Notes:

  • There is no distinction while parsing sub-commands, all commands & sub-commands are included in the args prop of output object in the given order.

    parser('docker container create --name=ubuntu01 ubuntu');
    {
      "args": ["docker", "container", "create", "ubuntu"],
      "opts": { "name": "ubuntu01" }
    }
  • Due to auto-inference of types, the values might be mismatched.

    The following example shows the color value returning as type number converted from hex value.

    $ node example.js --color=0xFFFF
    {
      "args": [],
      "opts": { "color": 65535 }
    }

    If you need the value as string, you can surround it with single or double quotes accordingly.

    $ node example.js --color='0xFFFF'
    {
      "args": [],
      "opts": { "color": "0xFFFF" }
    }
  • Currently true or false in the arguments will be parsed as string, Eg: --foo=false => { foo: 'false' }

    The auto-infer boolean types feature can be implemented later based on the user requests.

References

Command Line Interface Guidelines

License

Copyright (c) 2021, Thanga Ganapathy (MIT License).