2.0.3 • Published 5 days ago

@kearisp/cli v2.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 days ago

@kearisp/cli

Description

Command line interface for node.js

Installation

npm install @kearisp/cli

Usage

Command

import {Cli} from "@kearisp/cli";


const cli = new Cli();

cli.command("foo")
    .action(() => {
        return "Foo result";
    });

cli.run(process.argv).then((res) => {
    process.stdout.write(res);
}).catch((err) => {
    process.stderr.write(err.message);
});

Command argument

<bar> - required argument

[bar] - not required argument

ℹ️ The spread is not stable now

<...bars> - required spread argument

[...bars] - not required spread argument

cli.command("foo <foo1> [foo2]")
    .action((input: CommandInput) => {
        return `Foo result, with arguments foo1=${input.argument("foo1")} foo2=${input.argument("foo2")}`;
    });
cli.command("bar [...bars]")
    .action((input: CommandInput) => {
        return "Bar result, Bars: " + input.argument("bars").join(", ");
    });

Command option

Types:

  • string
  • boolean
  • number
cli.command("foo")
    .option("bar", {
        type: "string",
        alias: "b"
    })
    .option("init", {
        type: "boolean",
        alias: "i"
    })
    .action((input: CommandInput) => {
        const {
            bar = "",
            init = false
        } = input.options();

        return `Foo result, with options bar=${bar} init=${init.toString()}`;
    });

Help

cli.command("foo")
    .help({
        description: "Foo description"
    })
    .option("option", {
        alias: "o",
        description: "Option description"
    })
    .action((input) => {
        const {
            option = ""
        } = input.options();

        return `option=${option}`;
    });
./cli.js foo -h

Response:

Help:
Foo description

--option, -o - Option description

Completion

cli.command("foo <bar>")
    .completion("bar", () => ["value1", "value2", "value3"])
    .action((input) => {
        const {
            bar = ""
        } = input.arguments();

        return `Foo result, with argument bar=${bar}`;
    });

cli.command("completion script")
    .action(() => {
        return cli.completionScript();
    });
source <(your-script.js completion script)
2.0.3

5 days ago

2.0.2

15 days ago

2.0.1

17 days ago

2.0.0

18 days ago

1.0.7

2 months ago

1.0.6

3 months ago

1.0.5

4 months ago

1.0.4

5 months ago

1.0.3

6 months ago

1.0.2

8 months ago

1.0.1

9 months ago

1.0.0

9 months ago