# clap

> Command line argument parser

Latest version **3.1.1** (published 2022-02-07) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2022-02-07 |
| First published | 2014-02-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | ^12.20.0 \|\| ^14.13.0 \|\| >=15.0.0 |
| Dependencies | 1 |
| Unpacked size | 46.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Roman Dvornov |
| Maintainers | lahmatiy |
| Keywords | cli, command, option, argument, completion |

## Links

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

## Dependencies (1)

- [ansi-colors](https://npm.io/package/ansi-colors.md) ^4.1.1

## 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

- 3.1.1 (latest) — 2022-02-07
- 3.1.0 — 2022-02-07
- 3.0.0 — 2021-12-12
- 3.0.0-beta.1 — 2020-02-14
- 2.0.1 — 2019-12-16
- 2.0.0 — 2019-12-09
- 1.2.3 — 2017-09-20
- 1.2.2 — 2017-09-18
- 1.2.1 — 2017-09-18
- 1.2.0 — 2017-06-13
- 1.1.3 — 2017-03-16
- 1.1.2 — 2016-12-03
- 1.1.1 — 2016-05-10
- 1.1.0 — 2016-03-19
- 1.0.10 — 2015-12-16
- … 14 more at https://npm.io/package/clap/versions

## README

[![NPM version](https://img.shields.io/npm/v/clap.svg)](https://www.npmjs.com/package/clap)
[![Build Status](https://github.com/lahmatiy/clap/actions/workflows/build.yml/badge.svg)](https://github.com/lahmatiy/clap/actions/workflows/build.yml)
[![Coverage Status](https://coveralls.io/repos/github/lahmatiy/clap/badge.svg?branch=master)](https://coveralls.io/github/lahmatiy/clap?branch=master)

# Clap.js

A library for node.js to build command-line interfaces (CLI). With its help, making a simple CLI application is a trivial task. It equally excels in complex tools with a lot of subcommands and specific features. This library supports argument coercion and completion suggestion — typing the commands is much easier.

Inspired by [commander.js](https://github.com/tj/commander.js)

Features:

- TBD

## Usage

```
npm install clap
```

```js
const cli = require('clap');

const myCommand = cli.command('my-command [optional-arg]')
    .description('Optional description')
    .version('1.2.3')
    .option('-b, --bool', 'Bollean option')
    .option('--foo <foo>', 'Option with required argument')
    .option('--bar [bar]', 'Option with optional argument')
    .option('--baz [value]', 'Option with optional argument and normalize function',
        value => Number(value),
        123 // 123 is default
    )
    .action(function({ options, args, literalArgs }) {
        // options is an object with collected values
        // args goes before options
        // literal args goes after "--"
    });

myCommand.run();  // the same as "myCommnad.run(process.argv.slice(2))"
myCommand.run(['--foo', '123', '-b'])

// sub-commands
myCommand
    .command('nested')
        .option('-q, --quz', 'Some parameter', 'Default value')
        // ...
        .end()
    .command('another-command [arg1] [arg2]')
        // ...
        .command('level3-command')
            //...
```

## API

### Command

```
.command()
    // definition
    .description(value)
    .version(value, usage, description, action)
    .help(usage, description, action)
    .option(usage, description, ...options)
    .command(usageOrCommand)
    .extend(fn, ...options)
    .end()

    // argv processing pipeline handler setters
    .init(command, context)
    .applyConfig(context)
    .prerareContenxt(context)
    .action(context)

    // main methods
    .parse(argv, suggest)
    .run(argv)

    // misc
    .clone(deep)
    .createOptionValues()
    .getCommand(name)
    .getCommands()
    .getOption(name)
    .getOptions()
    .outputHelp()
```

### .option(usage, description, ...options)

There are two usage:

```
.option(usage, description, normalize, value)
.option(usage, description, options)
```

Where `options`:

```
{
    default: any,          // default value
    normalize: (value, oldValue) => { ... }, // any value for option is passing through this function and its result stores as option value
    shortcut: (value, oldValue) => { ... },  // for shortcut options, the handler is executed after the value is set, and its result (an object) is used as a source of values for other options
    action: () => { ... }, // for an action option, which breaks regular args processing and preform and action (e.g. show help or version)
    config: boolean        // mark option is about config and should be applied before `applyConfig()`
}
```

### Argv processing

- `init(command, context)`  // before arguments parsing
    - invoke action option and exit if any
- apply **config** options
- `applyConfig(context)`
- apply all the rest options
- `prepareContext(context)` // after arguments parsing
    - switch to next command -> command is prescending
        - `init(command, context)`
            - invoke action option and exit if any
        - apply **config** options
        - `applyConfig(context)`
        - apply all the rest options
        - `prepareContext(context)` // after arguments parsing
            - switch to next command
                - ...
            - `action(context)` -> command is target
    - `action(context)` -> command is target

## License

MIT

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