npm.io
0.9.9 • Published 2d ago

clinfer

Licence
MIT
Version
0.9.9
Deps
0
Size
53 kB
Vulns
0
Weekly
19

clinfer
clinfer JS (CLI infer-ence) : auto generate CLIs from code
clinfer on NPM JSR JSR Score github Built with the Deno Standard Library

clinfer brings CLI infer-ence to Node, Deno, and Bun. Pass it an object, a class, an ES module or a function, and watch it build your interface automatically:

  • Each field/property generates a CLI option (flag).
  • Each method/function generates a CLI command (with parameters as positional arguments).

Simply write your tool as a standard JS object, class or ES module, and hand it over to clinfer. It will automatically parse the command-line arguments, map them to your code, execute the right methods, and handle the help menu. You can then easily customize the generated help, add aliases, and fine-tune your CLI.

Overview : example with an object

docs/examples/demo/demo_object_lite.ts.png docs/examples/demo/demo_object_lite.ts.output.png

Quick start

Install clinfer with :

  • npm install clinfer
  • or with Deno : deno add clinfer or deno add jsr:@jersou/clinfer

Import clinfer function : import clinfer from "clinfer";

Init a script :

#!/usr/bin/env node
import clinfer from "clinfer";

const tool = {
  retry: 2,
  main(name = "none") {
    console.log(`main command name=${name}`);
    console.log(`retry=${this.retry}`);
  },
};

clinfer(tool);

The first line, the shebang, allows you to launch the script directly. The generated CLI :

Usage: <script path> [Options] [--] <name>

Options:
 -h, --help  Show this help [default: false]
     --retry                    [default: 2]

Features

  • No API to learn to build a basic CLI—just create a standard object.
  • You can then expand the object to optionally specify helpers, aliases, types, and more.
  • No need to define a separate type or schema for your CLI parameters; your input code is the schema.
  • Nest multiple objects to compose a complex CLI with multi-level commands, each with its own options.
  • Built-in support for JSON configuration files and environment variables to manage options.
  • The help is generated automatically:
    help image
  • Run the commands with options and arguments
#             ↓↓↓↓↓↓↓↓↓↓↓↓↓ options ↓↓↓↓↓↓↓↓↓↓↓↓  ↓ command ↓  ↓ cmd args ↓
$ ./simple.ts --dry-run --web-url=tttt --retry 4     down        true  14
down command { force: true, timeout: 14 } Tool { retry: 4, dryRun: true, webUrl: 'tttt' }

$ ./simple.ts down true 14                     #  ↓↓↓  default options from class init  ↓↓↓
down command { force: true, timeout: 14 } Tool { retry: 2, dryRun: false, webUrl: 'none' }

$ ./simple.ts --dry-run --webUrl=tttt # ← same case of the field name works too : --webUrl or --web-url
main command Tool { retry: 2, dryRun: true, webUrl: 'tttt' } # ← main is the default command

Documentation

The full documentation of clinfer is here : jersou.github.io/clinfer/.

Other examples

Several examples can be found in the docs/examples/ folder.

Example with a class
docs/examples/demo/demo_class_lite.ts.png docs/examples/demo/demo_class_lite.ts.output.png
Example with a function
docs/examples/demo/demo_function.ts.png docs/examples/demo/demo_function.ts.output.png
Example with a module (ESM)
docs/examples/demo/demo_module_lite.ts.png docs/examples/demo/demo_module_lite.ts.output.png
Full example with decorators (only for TypeScript & Deno for now)
docs/examples/demo/demo_class_decorator.ts.png docs/examples/demo/demo_class_decorator.ts.output.png
Full example without decorators
docs/examples/demo/demo_class.ts.png docs/examples/demo/demo_class.ts.output.png

Keywords