Colonel
A strongly typed and manageable CLI library.
There are a lot of libraries on NPM that simplify parsing command line arguments, but they all break down quickly without extending the architecture, since they focus on terseness rather than scalability.
Colonel is written in TypeScript, and offers a strongly typed framework for bootstrapping complex command line interfaces.
Installation
npm install --save colonel
Usage
/** * The interface for the options that a specific * command requires. */ /** * The actual command class, which receives the * options specified above. */ /** * One way to get the options required for the * command is by parsing the argument vector from * the shell. This needs to be implemented for * each options interface. */ /** * If options are deserialized from some format that * doesn't contain type information, we must provide * an object that normalizes that input into a valid * object. * * This is used below for extracting options from a * YAML configuration file. * * `Validator` means that the input object does * not need to result in a complete options object. */ // Now that our types are defined, we can wire// everything up.//// First, we need to create a list of options// providers, that collaborate to build up the// options from different sources. // To make sure that the command always receives a// complete `GreetOptions` object, and not a// `Partial<GreetOptions>`, we need to start with// a complete object and apply changes to that.//// So we provide an object with the default options.// If an option can be undefined and not have a// default value, the interface must declare that// with `field?: Type` or `field: Type | undefined`. // We can now assemble our option providers and the// command with a `CommandHandler`. // Finally, we pass in our handler into the main// `Program` class. // There is also a `MultiCommandHandler`, which can// be used to support multiple commands by looking// at the first command line argument. // Since the `MultiCommandHandler` accepts other instances// of `CommandHandler`, we can also pass in// `MultiCommandHandler` instances, creating a nested// command structure.//// Given an executable called `p`, the configuration below// enables these commands:// - `p` --> delegates to `p greet`// - `p greet` --> runs `GreetCommand`// - `p global` --> shows help for `p some-global-command`// - `p global some-command` --> runs `SomeCommand` // The `Program` can optionally be passed a `ProgramDisplay`// which handles help pages, error messages and more.// By default, a `ConsoleProgramDisplay` will be used, which// uses some fancy colors and ASCII characters to create// pretty output. // When we're ready to start the program, we simply need// to strip away the `['/path/to/node', '/path/to/cli.js']`// part of `process.argv`, and run `execute`program.execute