# microcli

> CLI scripts micro engine

Latest version **1.3.3** (published 2019-01-19) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install microcli
pnpm add microcli
yarn add microcli
bun add microcli
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2019-01-19 |
| First published | 2017-10-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.16.0 |
| Dependencies | 2 |
| Unpacked size | 184.2 KB |
| Known vulnerabilities | 0 (+6 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Pawel Galazka |
| Maintainers | pawelgalazka |
| Keywords | cli, option, parser, command, commander |

## Links

- npm: https://www.npmjs.com/package/microcli
- Repository: https://github.com/pawelgalazka/microcli
- Homepage: https://github.com/pawelgalazka/microcli#readme
- Issues: https://github.com/pawelgalazka/microcli/issues
- npm.io page: https://npm.io/package/microcli

## Dependencies (2)

- [lodash](https://npm.io/package/lodash.md) 4.17.11
- [microargs](https://npm.io/package/microargs.md) 1.1.2

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.3.3 (latest) — 2019-01-19
- 1.3.2 — 2019-01-11
- 1.3.1 — 2017-11-06
- 1.3.0 — 2017-11-06
- 1.2.0 — 2017-10-30
- 1.1.0 — 2017-10-28
- 1.0.0 — 2017-10-22

## README

# microcli ![node version](https://img.shields.io/node/v/microcli.svg) [![Build Status](https://travis-ci.org/pawelgalazka/microcli.svg?branch=master)](https://travis-ci.org/pawelgalazka/microcli) [![npm version](https://badge.fury.io/js/microcli.svg)](https://badge.fury.io/js/microcli)
CLI scripts micro engine

```js
#!/usr/bin/env node
const microcli = require('microcli')
const cli = microcli(process.argv, 'Script doc');
cli((options, p1, p2) => {
    console.log('OPTIONS', options)
    console.log('P1', p1)
    console.log('P2', p2)
})
```

```
$ script.js -a --foo=bar --boo abc def
OPTIONS {a: true, foo: 'bar', boo: true }
P1 abc
P2 def

$ script.js --help
Usage: script.js

Script doc
```

### Annotations

```js
#!/usr/bin/env node
const microcli = require('microcli')
const cli = microcli(process.argv, {
  description: 'Basic script description',
  params: ['p1', 'p2'],
  options: {
    a: 'description for a option',
    foo: 'description for foo option'
  },
  examples: 'some examples'
});

cli((options, p1, p2) => {
    console.log('OPTIONS', options)
    console.log('P1', p1)
    console.log('P2', p2)
})
```

```
$ script.js --help
Usage: script.js [options] [p1 p2]

Basic script description

Options:

    -a         description for a option
    --foo      description for foo option
    
Examples:

some examples
```

Annotations plays part also in validating process. So if
option which does not exist in annotations is provided, `microcli` will
throw an error:

```
$ script.js --bar
Illegal option: --bar
Available options: -a --foo
Type "script.js --help" for more information
```

Also each annotation is optional and custom annotations like `examples`
(basically other than description, params and options) will be treated
in `--help` content as additional header with string value.

### Commands
```
$ script.js status --foo abc 
OPTIONS {foo: true}
P abc

$ script.js branch --help
Usage: branch [options] [p]

Basic script description

$ script.js --foo abc
OPTIONS {foo: true}
P abc

```

```js
#!/usr/bin/env node
const microcli = require('microcli')

const main = microcli(process.argv, {
  description: 'base command',
  params: ['p'],
  options: {
    foo: 'foo option'
  }
})

const status = microcli(process.argv.slice(1), {
  description: 'Fake git status',
  params: ['p'],
  options: {
    foo: 'foo option'
  }
})

const branch = microcli(process.argv.slice(1), {
  description: 'Fake git branch',
  params: ['p'],
  options: {
    foo: 'foo option'
  }
})

switch (process.argv[2]) {
  case 'status':
    status((options, p) => {
      console.log('OPTIONS', options)
      console.log('P', p)
    })
    break

  case 'branch':
    branch((options, p) => {
      console.log('OPTIONS', options)
      console.log('P', p)
    })
    break

  default:
    main((options, p) => {
      console.log('OPTIONS', options)
      console.log('P', p)
    })
}
```

### Custom --help

You can provide `help` function to `cli` call, which can generate
custom help message, having annotations object:

```js
#!/usr/bin/env node
const microcli = require('microcli')
const cli = microcli(process.argv, {
  /* some annotations */
}, (scriptName, annotations, logger) => {
  logger.log('Custom --help message') 
});

cli((options, p1, p2) => {
    console.log('OPTIONS', options)
    console.log('P1', p1)
    console.log('P2', p2)
})
```

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