supercmd

SuperCommand is a framework to build command-line interfaces in node.js.

Usage no npm install needed!

<script type="module">
  import supercmd from 'https://cdn.skypack.dev/supercmd';
</script>

README

SuperCommand

SuperCommand is a framework to build command-line interfaces in node.js.

Usage

// bin/todo.js
import { SuperCommand } from '../'

const supercmd = new SuperCommand()

supercmd
  .cmd('list [options]')
  .description('List todos')
  .option('-s, --sort', 'Example of a boolean parameter')
  .option('-n, --num-items [num]', 'Example of an optional numeric parameter')
  .option('--filter [str]', 'Example of an optional string parameter')
  .action((ctx) => {
    // place command's code here
  })

await supercmd.parse(process.argv)

API

.cmd(str command)

Register a subcommand. A subcommand can take 0 - n arguments.

Wrap optional arguments values in [ and ] and required arguments values in < and >.

Example: mycommand <infile> [outfile]
This would register a command mycommand with a required argument infile and one optinal argument called outfile. The action method will be called with the arguments as second and third params.

.action((ctx, infile, outfile) => {

})

.cwd(str workingDir)

Set a working dir

.description(str description)

Describe what a program is for and what is does, show in the help page

.option(str arg, str description, any defaultValue, func func)

Register an option parameter. The param argument describes the parameter name, alias, value und value type. The syntax is [alias] [name] [value].

Alias is an one char long shortcut of the parameter and it is prefixed by one minus char. Example: -f.
Name is a parameter name. It is prefixed by two minus and its the only required part. A parameter can contain chars, numbers and a minus. Example: --foo, --foo-bar. The third part describes the parameter value and if it is a mandatory parameter or not. The default type is bool. The type must be enclosed by either angel bracets or square bracets. The angel bracets indicate the parameter as required, the square bracets indeicat it as optional.

Example for a required parameter of type boolean: -f --foo <bool>

Example for a optional parameter of type string: -b --bar [str]

Allowed types: str, num, arr, bool, json

.input(str name, str question, str type, any values)

Ask for input parameter. All input parameter are available as ctx.input.<name>

.usage(str description)

Describes program usage, shown in the help page.

.version(str version)

Set a program version, shown in the help page and if --version flag is set