4.0.0-rc.4 • Published 4 months ago

clipanion v4.0.0-rc.4

Weekly downloads
98,928
License
MIT
Repository
github
Last release
4 months ago

Clipanion

Type-safe CLI library with no dependencies

npm.io npm.io npm.io

Installation

yarn add clipanion

Why

  • Clipanion supports advanced typing mechanisms
  • Clipanion supports nested commands (yarn workspaces list)
  • Clipanion supports transparent option proxying without -- (for example yarn dlx eslint --fix)
  • Clipanion supports all option types you could think of (including negations, batches, ...)
  • Clipanion offers a Yup integration for increased validation capabilities
  • Clipanion generates an optimized state machine out of your commands
  • Clipanion generates good-looking help pages out of the box
  • Clipanion offers common optional command entries out-of-the-box (e.g. version command, help command)

Clipanion is used in Yarn with great success.

Recommended Usage

Note: This syntax assumes you have some way to compile decorators. TypeScript supports them via the experimentalDecorators setting, and Babel via the @babel/plugin-proposal-decorators plugin.

In essence you just need to declare a class that extends the Command abstract class, and implement the execute method. This function will then be called by Clipanion and its return value will be set as exit code by the engine (by default the exit code will be 0, which means success).

Options and command paths are set using the @Command decorators, unless you're in an environment that doesn't support them (in which case check the next section to see how to use the fallback syntax). Because you're in a regular class, you can even set default values to your options as you would with any other property!

import {Cli, Command} from 'clipanion';
import * as yup from 'yup';

class GreetCommand extends Command {
    @Command.Boolean(`-v,--verbose`)
    public verbose: boolean = false;

    @Command.String(`--name`)
    public name?: string;

    @Command.Path(`greet`)
    async execute() {
        if (typeof this.name === `undefined`) {
            this.context.stdout.write(`You're not registered.\n`);
        } else {
            this.context.stdout.write(`Hello, ${this.name}!\n`);
        }
    }
}

class FibonacciCommand extends Command {
    @Command.String({required: true})
    public a!: number;

    @Command.String({required: true})
    public b!: number;

    @Command.Path(`fibo`)
    async execute() {
        // ...
    }

    static schema = yup.object().shape({
        a: yup.number().integer(),
        b: yup.number().integer(),
    })
}

const cli = new Cli({
    binaryLabel: `My Utility`,
    binaryName: `bin`,
    binaryVersion: `1.0.0`,
});

cli.register(GreetCommand);
cli.register(FibonacciCommand);

cli.runExit(process.argv.slice(2), {
    ...Cli.defaultContext,
});

Fallback Usage

In case the primary syntax isn't available (for example because you want to avoid any kind of transpilation), a fallback syntax is available:

class GreetCommand extends Command {
    async execute() {
        // ...
    }
}

GreetCommand.addPath(`greet`);

GreetCommand.addOption(`boolean`, Command.Boolean(`-v,--verbose`));
GreetCommand.addOption(`name`, Command.String(`--name`));

Note that in this case the option variables never get assigned default values, so they may be undefined within the execute block.

Decorators

The optionNames parameters all indicate that you should put there a comma-separated list of option names (along with their leading -). For example, -v,--verbose is a valid parameter.

@Command.Path(segment1: string, segment2: string, ...)

Specifies through which CLI path should trigger the command. This decorator can only be set on the execute function itself, as it isn't linked to specific options.

Note that you can add as many paths as you want to a single command. By default it will be connected on the main entry point (empty path), but if you add even one explicit path this behavior will be disabled. If you still want the command to be available on both a named path and as a default entry point (for example yarn which is an alias for yarn install), simply call the decorator without segments:

@Command.Path(`install`)
@Command.Path()
async execute() {
  // ...
}

@Command.String({required?: boolean, tolerateBoolean?: boolean})

Specifies that the command accepts a positional argument. By default it will be required, but this can be toggled off.

tolerateBoolean specifies that the command will act like a boolean flag if it doesn't have a value. With this option on, an argument value can only be specified using =. It is off by default.

class RunCommand extends Command {
    @Command.String(`--inspect`, { tolerateBoolean: true })
    public debug: boolean | string = false;
    // ...
}

run --inspect
=> debug = true

run --inspect=1234
=> debug = "1234"

run --inspect 1234
=> invalid

@Command.String(optionNames: string)

Specifies that the command accepts an option that takes an argument.

@Command.Boolean(optionNames: string)

Specifies that the command accepts a boolean flag as an option.

@Command.Array(optionNames: string)

Specifies that the command accepts a set of string arguments (--arg value1 --arg value2).

Command Help Pages

Clipanion automatically adds support for the -h option to all the commands that you define. The information printed will come from the usage property attached to the class. For example, the following command:

class YarnAdd extends Command {
    static usage = Command.Usage({
        description: `remove dependencies from the project`,
        details: `
            This command will remove the specified packages from the current workspace. If the \`-A,--all\` option is set, the operation will be applied to all workspaces from the current project.
        `,
        examples: [[
            `Remove a dependency from the current project`,
            `yarn remove lodash`,
        ], [
            `Remove a dependency from all workspaces at once`,
            `yarn remove lodash --all`,
        ]],
    });
}

Will generate something like this:

npm.io

Note that the inline code blocks will be automatically highlighted.

Optional Built-in Command Entries

Clipanion offers common optional command entries out-of-the-box, under the Command.Entries namespace.

They have to be manually registered:

cli.register(Command.Entries.Help);
cli.register(Command.Entries.Version);

Help Command - General Help Page

Paths: -h, --help

The Command.Entries.Help command displays the list of commands available to the application, printing a block similar to the following.

npm.io

Version Command

Paths: -v, --version

The Command.Entries.Version command displays the version of the binary provided under binaryVersion when creating the CLI.

Composition

Commands can call each other by making use of their cli internal property:

class FooCommand extends Command {
    @Command.Path(`foo`)
    async execute() {
        this.context.stdout.write(`Hello World\n`);
    }
}

class BarCommand extends Command {
    @Command.Path(`bar`)
    async execute() {
        this.cli.run([`foo`]);
    }
}

Contexts

Commands share what is called a context. Contexts are a set of values defined when calling the run function from the CLI instance that will be made available to the commands via this.context. The default context contains properties for stdin, stdout, and stderr, but you can easily define a custom context that extends the default one:

import {BaseContext, Command} from 'clipanion';

type MyContext = BaseContext & {
    cwd: string;
};

class PwdCommand extends Command<MyContext> {
    async execute() {
        this.context.stdout.write(`${this.context.cwd}\n`);
    }
}

const cli = Cli.from<MyContext>([
    PwdCommands,
]);

cli.runExit(process.argv.slice(2), {
    ...Cli.defaultContext,
    cwd: process.cwd(),
});

Note that the context must be fully defined when calling run and runExit on the main CLI instance, but can be omitted or only partially specified when using this.cli.run (in which case only the specified fields will be changed).

License (MIT)

Copyright © 2019 Mael Nison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fourget@yuants/coreyarn-plugin-bundlehubcap-pm@njmaeff/publish-devtodomeniere-cliwesjet-simple-import-hooks@infinitebrahmanuniverse/nolb-clip@wesjet/cli@everything-registry/sub-chunk-1338begat@rednightgames/freyja@rednightgames/i18nsync-repos-cliswagger-buildwinterspecweread-spywesjetsanity-runner-clientshipixen-contentlayer-stackbit-yaml-generatorroadsign-clistackbit-config-wesjetspinner-memory-game-cliserver-forrh-verdacciovite-plugin-contentverdacciovsc-custom-css-clijust-extras@phyla/core@contentedjs/contentedhandy-imggithub-settergnote.tsmonotypomprovekerneltoolkong-portal-clihtml-w3c-validatorhow-cliinit-berrykarfia-docker-composemongoplotmonodeploymagicbinpacklintnext-auth-tinacmspebble-enginepdfcpdmreact-native-bumpnext-dev-contentlayernapi-rsnode-seaneural_compressor_ext_lab_customizedneural_compressor_ext_lab_customized_2qdksort-jsonc-clistrigirotate-pulumi-secretskpkuaaa-plugin-icsts-isolatetranslator-servertrpc-inittrpc-toolstrpc-v10-migrate-codemodtodone@apm-insight-web/upload-sourcemaps@aoi-js/cli@arcanis/sherlock@atlantis-lab/actl-typecheck@atlantis-lab/actl@atlantis-lab/actl-build@atlantis-lab/actl-check@atlantis-lab/actl-commit@atlantis-lab/actl-commitmsg@atlantis-lab/actl-cr-publisher@atlantis-lab/actl-format@atlantis-lab/actl-lint@atlantis-lab/actl-precommit@atlantis-lab/actl-test@atls/actl-typecheck@atls/actl-cli@atls/actl-commit@atls/actl-format@atls/actl-library@atls/actl-lint@atls/actl-renderer@atls/actl-service@atls/actl-test@albertoxamin/datadog-ci@birthdayresearch/contented@bigsaigon333/solve.io@bgotink/atelier@bufferapp/datadog-ci@clarigen/cli@chainsafe/napi-rs-cli@berry/builder@berry/core
4.0.0-rc.4

4 months ago

4.0.0-rc.3

11 months ago

4.0.0-rc.1

1 year ago

4.0.0-rc.2

1 year ago

3.2.1

2 years ago

3.2.0

2 years ago

3.2.0-rc.15

2 years ago

3.2.0-rc.16

2 years ago

3.2.0-rc.13

2 years ago

3.2.0-rc.14

2 years ago

3.2.0-rc.12

2 years ago

3.2.0-rc.11

3 years ago

3.2.0-rc.10

3 years ago

3.2.0-rc.6

3 years ago

3.2.0-rc.5

3 years ago

3.2.0-rc.8

3 years ago

3.2.0-rc.7

3 years ago

3.2.0-rc.9

3 years ago

3.2.0-rc.4

3 years ago

3.2.0-rc.2

3 years ago

3.2.0-rc.1

3 years ago

3.2.0-rc.3

3 years ago

3.1.0

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

3.0.0-rc.12

4 years ago

3.0.0-rc.11

4 years ago

3.0.0-rc.10

4 years ago

3.0.0-rc.9

4 years ago

3.0.0-rc.8

4 years ago

3.0.0-rc.7

4 years ago

3.0.0-rc.6

4 years ago

3.0.0-rc.2

4 years ago

3.0.0-rc.5

4 years ago

3.0.0-rc.4

4 years ago

3.0.0-rc.3

4 years ago

3.0.0-rc.1

4 years ago

2.6.2

4 years ago

2.6.1

4 years ago

2.6.0

4 years ago

2.5.0

4 years ago

2.4.4

4 years ago

2.4.3

4 years ago

2.4.2

5 years ago

2.4.1

5 years ago

2.4.0

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.1.6

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

2.0.0-rc.16

5 years ago

2.0.0-rc.15

5 years ago

2.0.0-rc.14

5 years ago

2.0.0-rc.13

5 years ago

2.0.0-rc.12

5 years ago

2.0.0-rc.11

5 years ago

2.0.0-rc.10

5 years ago

2.0.0-rc.9

5 years ago

2.0.0-rc.8

5 years ago

2.0.0-rc.6

5 years ago

2.0.0-rc.5

5 years ago

2.0.0-rc.4

5 years ago

2.0.0-rc.3

5 years ago

2.0.0-rc.2

5 years ago

2.0.0-rc.1

5 years ago

2.0.0-rc.0

5 years ago

0.17.1

5 years ago

0.17.0

5 years ago

0.16.1

5 years ago

0.16.0

5 years ago

0.15.0

6 years ago

0.14.1

6 years ago

0.14.0

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago