0.15.0 • Published 2 months ago

cli-er v0.15.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

cli.js:

import Cli from "cli-er";

const definition = {...}

new Cli(definition).run();

Invocation:

node cli.js [namespace(s)|command] [OPTIONS]

Example

Given the following definition (docker.js):

const definition = {
  builder: {
    description: "Manage builds",
    options: {
      build: {
        description: "Build an image from a Dockerfile",
        options: {
          source: {
            positional: 0,
            required: true,
            description: "Path or Url to the Dockerfile"
          }
        }
      },
    },
  },
  debug: {
    type: "boolean",
    aliases: ["D", "debug"],
    default: false,
  },
};

it will allow us to structure the code as follows:

.
├─ docker.js
└─ builder
   └── build.js

so we can then execute:

node docker.js builder build . -- someother-external-option

which will try to invoke, in order: 1. /builder/build/index.js 2. /builder/build.js 3. /builder/index.js 4. /builder.js 5. /index.js 6. /docker.js

with the parsed options (only the first two are default imports, the rest are named imports using the command name, in this case build). This allows us to organize and structure the logic nicely.

You can check the full docker-based example for a more in-depth demo.

Features

  • Intl support: support for internationalized messages.
  • Routing: routes are generated where command handlers are expected to be found.
  • Bash completion: a command is created to generate the bash-completions script for the cli.
  • Debug mode: validate the definition and options, especially when upgrading to a new version.
  • Typescript support: build the cli with typescript.

Installation

npm install cli-er

Usage

cli-er default-exports a class, which takes in a definition object and an optional argument CliOptions. The available methods are the following:

  • parse(args): parse the given arguments and return an object containing the options, errors and calculated location.
  • run(args?): parse the given arguments and execute the corresponding script found in the calculated location. Integrates help and version generation.
  • help(location?): generate help based on the definition. Can be scoped to a namespace/command.
  • version(): generate a formatted version of the application's version.
  • completions(): output bash-completion script contents.

Glossary

  • Namespace: is used to group commands, but cannot be invoked. Can contain other namespaces, commands or options.
  • Command: Is the final invocable element. Can only contain options.
  • Option: arguments that hold values of different types, like string, boolean, list...
0.15.0

2 months ago

0.14.0

5 months ago

0.11.0

9 months ago

0.12.0

8 months ago

0.13.0

7 months ago

0.12.1

8 months ago

0.10.0

1 year ago

0.10.1

1 year ago

0.9.2

1 year ago

0.9.0

1 year ago

0.9.1

1 year ago

0.8.0

1 year ago

0.7.2

1 year ago

0.7.1

1 year ago

0.7.0

1 year ago

0.6.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago