/litargs

The easiest CLI command line parser.

Primary LanguageTypeScriptMIT LicenseMIT

Litargs


typescript node.js npm npm GitHub issues GitHub forks GitHub stars GitHub license

Would you like to parse arguments more easily?

Highlights:flashlight:

  • The easiest CLI command line parser
  • Recommended for those who want simple parsing results
  • Easy to set options
  • Automatically create help

Install

npm install litargs

Usage

  • index.js
//Example of a command to move a file
const { Litargs } = require('litargs');
const fs = require('fs');

Litargs.command(
    'move',
    2,
    { args: ['source', 'destination'], detail: 'Move a file' },
    (args, option) => {
        if (option.cp) {
            fs.copyFileSync(args[0], args[1]);
        } else {
            fs.renameSync(args[0], args[1]);
        }
    }
)
    .option('cp', 0, { detail: 'copy' })
    .parse(process.argv.slice(2).join(' '));

Litargs.execute();
  • Command Line
$ node index.js move /Users/hoge.txt /Users/fuga.txt --cp
  • help
Commands:
help                    Display a list of commands and options


move    [source, destination]   Move a file
        Options:
        --cp                    copy

API

Litargs.command(name<string>, argumentCount<number>, description<{args?: string[], detail: string}>, handler<(args<string>, option<{[optionName: string]: string[]|boolean>}): unknown>)

Add the command. Arguments are the name of the command, the number of arguments, a description of the command, and the function to be executed. The handler can accept arguments and options. For description, set the name of the argument in args and a concrete description in detail. If the expected number of arguments is 0, there is no need to set args.

Litargs.option(name<string>, argumentCount<number>, description<args?: string[], detail: string>)

Commands and options are added in the method chain.

Litargs.command(...).option(...).option(...).command(...).option(...)...

The option method will be applied to the last command added. The basic settings are the same as for the command method. Options that require arguments are prefixed with "-", and options that do not require arguments are prefixed with "--". This does not need to be attached to "name", but it is required on the command line.

Litargs.alias(name<string>)

The alias method will be applied to the last command added. You can give different names to the commands.

Litargs.parse(parsedString<string>)

Add this to the end of the method chain. For use with the node.js cli, specify process.argv.slice(2).join(' '). This method returns a simple parsing result.

Litargs.execute()

Execute the registered handler. This should not be done before parse.

Issues

If you find a bug or problem, please open an issue!:bug:

Author

LICENSE

This project is licensed under the MIT License - see the LICENSE file for details.