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

cli-input-parser

v0.2.3

Published

A configuration based parser for command line input and arguments.

Downloads

7

Readme

CLI Input Parser

A configuration-forward TypeScript library to parse command line inputs with support for flagged and non-flagged arguments.

Table of Contents

  1. Summary
  2. Installation
  3. Usage
  4. Error Handling
  5. Contributing
  6. License

Summary

The CLI Input Parser allows you to define a configuration for supported commands and their arguments, and then parse input strings based on this configuration. It supports flagged arguments (starting with a dash) and non-flagged arguments, with optional validation using string lists or regular expressions.

Installation

To install the CLI Input Parser, use npm:

npm install cli-input-parser

Usage

Step 1: Create a Configuration

The configuration specifies a list of supported commands. Each command can have optional flagged arguments. Both flagged and non-flagged arguments can specify their expected/accepted formats using either a list of strings or a regular expression. Flagged arguments are those that start with a dash (-). Below are some examples:

const config: ParserConfig = {
    supportedCommands: [
        // example1 -m/--mode <string> -n/--number <number> [arg1 ... argN]
        {
            name: 'example1',
            flags: [
                {
                    character: 'm', // -m
                    verbose: 'mode', // --mode
                    accepts: ['dev', 'prod'], // Accepts 'dev' or 'prod'
                },
                {
                    character: 'n', // -n
                    verbose: 'number', // --number
                    accepts: /^(?:[1-9]|[1-9][0-9])$/, // Accepts 1-99
                },
            ],
            accepts: ['non', 'flagged', 'arguments'],
        },
        // example2 -v/--verbose
        {
            name: 'example2',
            flags: [
                {
                    character: 'v', // -v
                    verbose: 'verbose', // --verbose
                },
            ],
        },
    ],
};

Step 2: Create a Parser

A valid configuration must be passed to the parser upon construction.

import { CLIParser } from 'cli-input-parser';
const parser = new CLIParser(config);

Step 3: Parse Some Input

Use the parser to parse input strings based on the configuration.

const input = 'example1 -m dev -n 42 argument1 argument2';
const parsed = parser.parse(input);

Step 4: Use the Parsed Input

The parsed input separates flagged and non-flagged arguments. Here is the type definition of the parsed input:

export interface ParsedInput {
    command: string;
    flaggedArgs: Array<{
        flag: string;
        value?: unknown;
    }>;
    flaglessArgs: unknown[];
}

To access specific flagged arguments:

// Example to get the -m/--mode arg from example1 above.
let mode = '';
if (parsed.hasFlagWithValue('m')) {
    mode = parsed.getFlagValue('m');
}

// Example to check if -v/--verbose flag is present from example2 above.
let verbose = parsed.hasFlag('v');

Error Handling

Help

Any command passed with the -h or --help argument will throw the CLINeedsHelp error. Currently, it is the user's responsibility to handle this error. Future versions may include built-in help messages in the configuration.

Other Errors

All other errors when calling parse will throw the CLIParseError error.

Contributing

Contributions are welcome! Please submit pull requests or issues to the repository.

License

This project is licensed under the MIT License.