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

clia

v2024.6.1

Published

Command line arguments parser

Downloads

41

Readme

clia

Command line arguments parser. Similar to command-line-args, getopts and nopt, but quite smaller with less files and jokes.

You can give it a quick test in your browser on runkit with

const conf = clia('hello -a -ab -d world'.split(' '))

Like the other parsers, clia follows the same syntax conventions documented in design docs with lots of tests/examples here.

usage

Example command line input:

node your-app hello -a -ab -d world

In your-app you get parsed command line arguments as follows:

const clia = require('clia')

const conf = clia(process.argv.slice(2))

conf === {
  // arguments before any options
  plain: [ 'hello' ], 
  // options saved in opt (eg. --a -bd)
  opt: { a: true, b: true, d: true }, 
  // arguments after options are tagged with the last option (eg -d world, or --d world)
  // argument --key=value also saved in args, eg --d=world
  args: { d: [ 'world' ] }, 
  // the first value of each args property, so that you can use arg.prop instead of args.prop[0]
  arg: { d: 'world' }, 
}

alias

Pass a second argument to clia to specify aliases:

clia('run -o yaml --d=/usr/bin --fruit=mango'.split(' ')
                , ['output', 'directory', 'fruit'])

yields

{
    arg: {
        o: 'yaml', output: 'yaml',
        d: '/usr/bin', directory: '/usr/bin',
        fruit: 'mango'
    },
    args: {
        o: ['yaml'], output: ['yaml'],
        d: ['/usr/bin'], directory: ['/usr/bin'],
        // note key-value doesn't set option
        // even when kv/value matches alias 
        fruit: ['mango']
    },
    // note key-value doesn't set opt
    // even when kv/value is short option that has an alias
    opt: { o: true, output: true },
    plain: ['run']
}

edge cases

Spaces are trimmed from inputs.

Empty or non-string inputs are ignored.

Inputs that contain __proto__ or prototype are ignored. (To prevent prototype pollution.)

If there are any errors, there will be an errors property in the return value

Example invalid command line input:

node your-app.js valid --ok=yes prototype last-token

yields

{
    errors: [
        'One or more args were excluded from parsing. Reason: Not a string, string is empty or spaces only, string contains __proto__ or prototype.'
    ],
    arg: { ok: 'yes' },
    args: { ok: ['yes'] },
    opt: {},
    plain: ['valid', 'last-token']
}

It is recommended that you check for any input errors.

// in main.js/index.js
const conf = clia(process.argv.slice(2))

if(conf.errors){
    // graceful exit
    console.log('Could not parse command line input, errors:')
    console.log(conf.errors)
    require('process').exitCode(1)
    return
}

When -- is encountered, it is ignored. All subsequent inputs are treated as arguments even if they start with -.

Key-values with missing key or value are saved as is, eg:

option --store= yields: { .. opt: { 'store=': true }

option --=pet yields: { .. opt: { '=pet': true }

example

An example of where clia is used to parse command line arguments, with "autocomplete" (Cli option not found. Did you mean ___) can be found here