# nestjs-eclih

> Build beautify, powerful, elegant CLI with Nestjs

Latest version **0.0.15** (published 2022-07-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-eclih
pnpm add nestjs-eclih
yarn add nestjs-eclih
bun add nestjs-eclih
```

## Health

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

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.15 |
| Published | 2022-07-16 |
| First published | 2021-02-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 116.8 KB |
| Known vulnerabilities | 0 (+3 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Shihao Xia |
| Maintainers | charlesxsh |

## Links

- npm: https://www.npmjs.com/package/nestjs-eclih
- Repository: https://github.com/charlesxsh/nestjs-eclih
- Homepage: https://github.com/charlesxsh/nestjs-eclih#readme
- Issues: https://github.com/charlesxsh/nestjs-eclih/issues
- npm.io page: https://npm.io/package/nestjs-eclih

## Dependencies (4)

- [commander](https://npm.io/package/commander.md) 9.4.0
- [@nestjs/core](https://npm.io/package/@nestjs/core.md) 9.0.3
- [@nestjs/common](https://npm.io/package/@nestjs/common.md) 9.0.3
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.1.13

## Recent versions

- 0.0.15 (latest) — 2022-07-16
- 1.0.0 — 2021-08-19
- 0.0.14 — 2021-08-04
- 0.0.13 — 2021-03-19
- 0.0.12 — 2021-03-17
- 0.0.11 — 2021-03-11
- 0.0.10 — 2021-03-10
- 0.0.9 — 2021-03-09
- 0.0.8 — 2021-03-09
- 0.0.7 — 2021-03-09
- 0.0.6 — 2021-03-04
- 0.0.5 — 2021-03-04
- 0.0.4 — 2021-02-23
- 0.0.3 — 2021-02-23
- 0.0.2 — 2021-02-23
- … 1 more at https://npm.io/package/nestjs-eclih/versions

## README

- [Nestjs Elegant Command Line Interface Hammer](#nestjs-elegant-command-line-interface-hammer)
  - [Commander.js](#commanderjs)
  - [Features](#features)
    - [Full support of Commander.js](#full-support-of-commanderjs)
    - [Partial Command Definition](#partial-command-definition)
    - [Command of Command](#command-of-command)
  - [Example](#example)
  - [Tutorial](#tutorial)
    - [@CommandProvider](#commandprovider)
      - [Examples](#examples)
    - [@Command](#command)
      - [Examples](#examples-1)
    - [CommandConfig](#commandconfig)
    - [OptionConfig](#optionconfig)

# Nestjs Elegant Command Line Interface Hammer
[![NPM Version](http://img.shields.io/npm/v/nestjs-eclih.svg?style=flat)](https://www.npmjs.org/package/nestjs-eclih)
[![NPM Downloads](https://img.shields.io/npm/dm/nestjs-eclih.svg?style=flat)](https://npmcharts.com/compare/nestjs-eclih?minimal=true)
[![NPM License](https://img.shields.io/npm/l/all-contributors.svg?style=flat)](https://github.com/charlesxsh/nestjs-eclih/blob/master/LICENSE)


nestjs-eclih aims to provide the tools to build powerful CLI with simple(only two) decorators.
Nestjs provides the powerful dependency injection system. Commander provides the flexible and concrete CLI tools. nestjs-commander combines their advantanges together!

## Commander.js
nestjs-eclih utilized [commander.js](https://github.com/tj/commander.js) as CLI driver. All grammers of command and option are excatly same.

## Features

### Full support of Commander.js
See [examples/hello.ts](./examples/hello.ts)


### Partial Command Definition 
See [examples/provider-of-provider](./examples/command-partial-define.ts)

### Command of Command
See [examples/provider-of-provider](./examples/provider-of-provider.ts)

## Example

Create a typescript file hello.ts with following:
```ts
import { Module } from "@nestjs/common";
import { CommandProvider, Command, CommanderModule, bootstrapCli } from "nestjs-eclih";

@CommandProvider()
class HelloProvider {

    @Command({
      options: [
        { nameAndArgs: "-n, --name <name>" }
      ]
    })
    hello(options){
        console.log("hello", options.name);
    }
}

@Module({
    imports: [
        CommanderModule
    ],
    providers:[
        HelloProvider
    ]
})
export class AppModule {}


bootstrapCli(AppModule);
```

Now your first CLI is ready!

```bash
$ ts-node hello.ts --help
Usage: hello [options] [command]

Options:
  -h, --help      display help for command

Commands:
  hello [options]
  help [command]  display help for command

$ ts-node hello.ts hello -n husky
hello husky
```

See more examples in [examples](./examples)


## Tutorial

### @CommandProvider
@CommandProvider is a class decorator, it accepts null, a string or a [CommandConfig](#commandconfig)
A null/undefined (which means just `@CommandProvider()`)
A string infers a `CommandConfig` and its `nameAndArgs` is class name.

#### Examples 

```ts
@CommanderProvider()
class SomeClass {}

// So anything under this class will be the subcommand of command hello
@CommanderProvider({
  nameAndArgs: "hello",
  description: "hello's description",
  alias: "h"
})
class SomeClass {}
```

### @Command
@Command is a class methold decorator, it accepts either a string or a [CommandConfig](#commandconfig)
A string infers a `CommandConfig` and its `nameAndArgs` is method name.

#### Examples 

```ts
@CommanderProvider()
class SomeClass {

  @Command({
    // Omit nameAndArgs, since by default it is the method name
    options: [
      { nameAndArgs: "-n, --name <name>" }
    ]
  })
  hello(options){
      console.log("hello", options.name);
  }
}

```
### CommandConfig
```ts
interface CommandConfig {
  nameAndArgs?: string;
  description?: string;
  options?: OptionConfig[];
  alias?: string;
  aliases?: string[];
}
```

### OptionConfig
```ts
interface OptionConfig {
  nameAndArgs: string; 
  description?: string;
  mandatory?: boolean;
  default?: string;
  choices?: string[];
}

```

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