# @corteks/clify

> Ease cli entry point scaffold with decorators

Latest version **0.4.0** (published 2021-01-07) · GPL-3.0-or-later license · 0 weekly downloads

## Install

```sh
npm install @corteks/clify
pnpm add @corteks/clify
yarn add @corteks/clify
bun add @corteks/clify
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2021-01-07 |
| First published | 2020-05-24 |
| Weekly downloads | 0 |
| License | GPL-3.0-or-later |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 12.16.2 |
| Dependencies | 2 |
| Unpacked size | 49.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Raphael67 |
| Maintainers | raphael_a |
| Keywords | command-line, decorators |

## Links

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

## Dependencies (2)

- [yargs](https://npm.io/package/yargs.md) ^15.3.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 0.4.0 (latest) — 2021-01-07
- 0.3.0 — 2021-01-07
- 0.2.1 — 2020-09-24
- 0.2.0 — 2020-09-24
- 0.1.3 — 2020-05-24
- 0.1.2 — 2020-05-24
- 0.1.1 — 2020-05-24
- 0.1.0 — 2020-05-24

## README

Provide little tool to create your CLI main class in typescript.

## functionalities:

-   Parse command line arguments by adding decorator to your Main class
-   Fill your Main class properties with the command line argument values
-   Create help by analyzing your Main class properties
-   Instantiate and run your Main class
-   Ensure execution of a cleaning function before process exit
-   Handle exit codes

The command line argument parsing is provided by [Yargs](https://www.npmjs.com/package/yargs) under the hood.

## 1. installation

```
npm i --save @corteks/clify
```

Please be sure that your tsconfig file is configured as follow:

```jsonc
"target": "es6", // es6 minimum is mandatory if you need to address older version you will have to recompile this module
"experimentalDecorators": true, // this is mandatory to use decorators
"emitDecoratorMetadata": true // this is mandatory if you want clify to infer argument type from properties
```

## 2. usage

```ts
// Main.ts
import {
    CliMainClass,
    CliParameter,
    CliMain,
    KeyPress,
    Modifiers,
} from '@corteks/clify';

// Decorate your Main class
// Extend the base class
@CliMain
class Main extends CliMainClass {
    // Decorate your parameters with Yargs.Options
    // Mandatory parameters without default value have to be undefined
    // Alias are automatically generated from the first letter of the property name
    @CliParameter({ demandOption: true, description: 'parameter1' })
    private parameter1: string | undefined = undefined;

    // Parameter type is automatically defined from the property type
    // Default yargs options can be override
    @CliParameter({ alias: 'p2' })
    private parameter2: string = 'default';

    // Implement the main() method inherited from the base class
    // this will be automatically launch at startup
    // all properties will have the values defined by the user from the command line
    // Return an exit code at the end
    async main(): Promise<number> {
        console.log(this.parameter1);
        console.log(this.parameter2);
        console.log(this._); // All inline parameters are stored as a string[].

        await new Promise((resolve, reject) => {
            setTimeout(() => {
                console.log('timeout');
                resolve();
            }, 5000);
        });

        return 0;
    }

    // Bind function to keyPress event
    // All key bindings are console logged at the start of the software
    // CTRL+c is registered by default and call Main.stop
    @KeyPress('space', Modifiers.NONE, 'This is a test function')
    test() {
        console.log('Space has been pressed.');
    }

    // Implement the stop() method inherited from the base class
    // This function will always be run before the process exit
    // It will receive the exit code of the main() function as parameter
    // The exit code return by the stop() function will be used as the exit code of the process
    async stop(exitCode: number): Promise<number> {
        console.log(exitCode);
        return exitCode;
    }
}
```

If you do not want to exit when the main return, for example when you launch an async process, use the `done(exitCode: number) => void` callback passed as the `main` function parameter:

```ts
// Main.ts
// ...
@CliMain
class Main extends CliMainClass {
    // ...
    async main(done: (exitCode: number) => void): Promise<number> {
        myAsyncProcess()
            .then(() => {
                done(0)
            })
            .catch(() => {
                done(1)
            })

        return 0;
    }

    // ...
    async stop(exitCode: number): Promise<number> {
        console.log(exitCode);
        return exitCode;
    }
}
```

## Results

```
bash> ./my_cli

Options:
  --help              Affiche l'aide                                   [booléen]
  --version           Affiche le numéro de version                     [booléen]
  --parameter1, -p    parameter1                                        [requis]
  --parameter2, --p2  parameter2     [chaîne de caractères] [défaut : "default"]

Argument requis manquant : parameter1
```

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