JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 123011
  • Score
    100M100P100Q158861F
  • License MIT

commandline option parser

Package Exports

  • commandpost

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (commandpost) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

commandpost Circle CI

commandpost is a command-line option parser. This library inspired by commander.

commander is very user friendly, but not TypeScript friendly. commandpost is improve it. Of course, commandpost can also be used from ordinary JavaScript program. 👍

Installation

$ npm install --save commandpost

How to use

Basic usage

$ cat cli.ts
import * as commandpost from "commandpost";

let root = commandpost
    .create<{ spice: string[]; }, { food: string; }>("dinner <food>")
    .version("1.0.0", "-v, --version")
    .description("today's dinner!")
    .option("-s, --spice <name>", "What spice do you want? default: pepper")
    .action((opts, args) => {
        console.log(`Your dinner is ${args.food} with ${opts.spice[0] || "pepper"}!`);
    });

commandpost
    .exec(root, process.argv)
    .catch(err => {
        if (err instanceof Error) {
            console.error(err.stack);
        } else {
            console.error(err);
        }
        process.exit(1);
    });

$ node cli.js --help
  Usage: dinner [options] [--] <food>

  Options:

    -s, --spice <name>  What spice do you want? default: pepper

$ node cli.js -s "soy sause" "fillet steak"
Your dinner is fillet steak with soy sause!

Command

top level command is created by commandpost.create function.

commandpost can have a sub command. sub command is created by topLevelCommand.subCommand method. like this.

commandpost can configure several items. e.g. version information, app description, CLI usage and help message. I recommend that you should setup .version and .description. Usually, automatic generated help message satisfy you.

Option

// shorthand style & formal style option with required parameter. option value is convert to string[].
cmd.option("-c, --config <configFile>", "Read setting from specified config file path");

// option with optional parameter. option value is convert to string[].
cmd.option("-c, --config [configFile]", "Read setting from specified config file path");

// option without parameter. option value is convert to boolean. default false.
cmd.option("--suppress-warning", "Suppress warning");

// option with `--no-` prefix. option value is convert to boolean. default true.
cmd.option("--no-type-checking", "Type checking disabled");

If you want to handling unknown options, You can use .allowUnknownOption method.

Argument

// required argument
commandpost.create<{}, { food: string; }>("dinner <food>");

// optonal argument
commandpost.create<{}, { food: string; }>("dinner [food]");

// variadic argument
commandpost.create<{}, { foods: string[]; }>("dinner <food...>");

Examples

Contributing

This package's author vvakame is not native english speaker. My first language is Japanese. If you find incorrect english, please send me a pull request.