# @timhall/cli

> Composable CLI utilities

Latest version **0.5.0** (published 2020-01-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @timhall/cli
pnpm add @timhall/cli
yarn add @timhall/cli
bun add @timhall/cli
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2020-01-12 |
| First published | 2020-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 45.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Tim Hall |
| Maintainers | timhall |

## Links

- npm: https://www.npmjs.com/package/@timhall/cli
- Repository: https://github.com/timhall/cli
- Homepage: https://github.com/timhall/cli#readme
- Issues: https://github.com/timhall/cli/issues
- npm.io page: https://npm.io/package/@timhall/cli

## Dependencies (3)

- [mri](https://npm.io/package/mri.md) ^1.1.4
- [meant](https://npm.io/package/meant.md) ^1.0.1
- [@timhall/ansi-colors](https://npm.io/package/@timhall/ansi-colors.md) ^5.0.0

## Recent versions

- 0.5.0 (latest) — 2020-01-12
- 0.4.1 — 2020-01-12
- 0.4.0 — 2020-01-12
- 0.3.0 — 2020-01-11
- 0.2.0 — 2020-01-06
- 0.1.1 — 2020-01-04
- 0.1.0 — 2020-01-01

## README

# @timhall/cli

## cli({ name, version, subcommands })

Prepare `help`, `version`, and `run` for commands.

```ts
import { cli, commands, run } from '@timhall/cli';
import { name, version } from '../../package.json';

const subcommands = commands({
  init: { run: argv => { /* ... */ }, description: '...' }
  commit: { load: () => import('./example-commit'), description: '...' },
  // ...
});

const example = cli({ name, version, subcommands });

run(name, async () => {
  const argv = process.argv.slice(2);
  await example.run(argv);
});
```

```txt
> example --help
example v(version)

Usage: example <command>

Commands:
  - init    ...
  - commit  ...

example help <command>  Help on <command>
example -v / --version  Show current version

> example -v
(version)

> example help init
(calls init.run with ['--help'])

> example int
ERROR Unknown command "int", did you mean "init"?

Available commands are "init" and "commit".
Try "example help" for more information.

> example commit -m "Add cli"
(calls commit.default with ['-m', 'Add cli'])
```

## commands(subcommands)

Load and run subcommand functions

Example:

```ts
// ./example-commit
export default async function commit(argv: string[]) {
  // ...
}
```

```ts
import { commands } from '@timhall/cli';

const subcommands = commands({
  init: {
    run(argv: string[]) {
      // Define command directly
    },
    description: 'Initialize a new repository'
  },
  commit: {
    // Only load and run commit command if called
    load: () => import('./example-commit'),
    description: 'Commit change to repository'
  }
});

console.log(subcommands.list);
// {
//   init: 'Initialize a new repository',
//   commit: 'Commit change to repository'
// }

async function main() {
  await subcommands.run(['commit', '-m', 'Added cli']);

  // 1. Load ./example-commit
  // 2. Run with ['-m', 'Added cli']
}
```

## run([title], fn)

Run the given function with proper process management and error handling.

---
_Source: https://npm.io/package/@timhall/cli · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
