# @jkhong/cli-js

> cli-js allows you to quickly set up a command line interface.

Latest version **1.2.0** (published 2023-03-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install @jkhong/cli-js
pnpm add @jkhong/cli-js
yarn add @jkhong/cli-js
bun add @jkhong/cli-js
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.0 |
| Published | 2023-03-05 |
| First published | 2019-04-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Julien Khong |
| Maintainers | jkhong |

## Links

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

## Dependencies (4)

- [colors](https://npm.io/package/colors.md) 1.3.3
- [minimist](https://npm.io/package/minimist.md) 1.2.8
- [simpl-schema](https://npm.io/package/simpl-schema.md) 1.5.5
- [@jkhong/devutils](https://npm.io/package/@jkhong/devutils.md) 1.5.1

## Recent versions

- 1.2.0 (latest) — 2023-03-05
- 1.1.2 — 2023-02-28
- 1.1.1 — 2023-02-28
- 1.1.0 — 2023-02-28
- 1.0.1 — 2019-04-30
- 1.0.0 — 2019-04-27

## README

# Purpose

cli-js allows you to quickly set up a command line interface.

Let's say you want to create a command line program (myBoilerplateGenerator) which creates your own boiler plate for:
 - react
 - react native
 - meteor

Provided you have written the code to generate your boiler plates, you can use them this way:

```sh
./myBoilerplateGenerator generateReact --projectName myNewReactProject --namespace NRP --useRedux

./myBoilerplateGenerator generateReactNative --projectName myNewReactNativeProject

./myBoilerplateGenerator generateMeteor --projectName myNewMeteorProject --namespace NRP --version 1.6
```

# How to create a minimum cli

create your myBoilerplateGenerator file (no extension)

```javascript
#!/usr/bin/env node
const CLI = require("@jkhong/cli-js");

try {
    // minimal manual content 
    const manualContent = {
        appName: "Boiler plate generator",
        binName: "myBoilerplateGenerator",
        actionsGroups: []
    };
    const cli = new CLI.Interface();
    cli.setManualContent(manualContent);
    cli.launch();
}
catch (e) {
    console.log(e);
}
```

make your script executable
```sh
chmod +x myBoilerplateGenerator
```

and run the following commands
```sh
# notifies no action is given
./myBoilerplateGenerator

# displays default help menu
./myBoilerplateGenerator help
```

# How to plug your code to the cli

configure the manualContent object

```javascript
// import your code
const ReactActions = require('./react.js');
const ReactNativeActions = require('./reactNative.js');
const MeteorActions = require('./meteor.js');

// use your code in methods to be plugged to the manual
// the methods get the user input values with the cliOptions object
function ReactPlug(cliOptions){
    ReactActions.genBoilerPlate(
        cliOptions.getSwitchValue("projectName"),
        cliOptions.getSwitchValue("namespace"),
        cliOptions.getSwitchValue("useRedux")
    );
}
function ReactNativePlug(cliOptions){
    ReactNativeActions.genBoilerPlate(
        cliOptions.getSwitchValue("projectName")
    );
}
function MeteorPlug(cliOptions){
    MeteorActions.genBoilerPlate(
        cliOptions.getSwitchValue("projectName"),
        cliOptions.getSwitchValue("namespace"),
        cliOptions.getSwitchValue("version")
    );
}

// build the manual content
const manualContent = {
    appName: "Boiler plate generator",
    binName: "myBoilerplateGenerator",
    actionsGroups: [
        {
            name: "react actions",
            desc: "some cli utils for react projects",
            actions: [{
                name: "generateReact",
                desc: "generate a react boiler plate",
                switches: [
                    {
                        name: "projectName",
                        desc: "the name of the project"
                    },
                    {
                        name: "namespace",
                        desc: "the namespace of the project"
                    },
                    {  // optional switch due to the default value
                        name: "useRedux",
                        desc: "configure for redux",
                        default: {value: false}
                    }
                ],
                action: ReactPlug
            }
            ]
        },
        {
            name: "react native actions",
            desc: "some cli utils for react native projects",
            actions: [{
                name: "generateReactNative",
                desc: "generate a react native boiler plate",
                switches: [
                    {
                        name: "projectName",
                        desc: "the name of the project"
                    }
                ],
                action: ReactNativePlug
            }
            ]
        },
        {
            name: "meteor actions",
            desc: "some cli utils for meteor projects",
            actions: [{
                name: "generateMeteor",
                desc: "generate a meteor boiler plate",
                switches: [
                    {
                        name: "projectName",
                        desc: "the name of the project"
                    },
                    {
                        name: "namespace",
                        desc: "the namespace of the project"
                    },
                    {  // optional switch due to the default value
                        name: "version",
                        desc: "meteor version to use",
                        default: {value: 1.5}
                    }
                ],
                action: MeteorPlug
            }
            ]
        }
    ]
};
```

# Customize your cli

## The manual
You can add optional values to the header like a subtitle and the version and add to help an about.

```javascript
const manualContent = {
    appName: "Boiler plate generator",
    binName: "myBoilerplateGenerator",
    appSubTitle: " the best on the market place",
    appVersion: `v.1.2.3`,
    about: "some descriptive information about your script"
};
```

If you don't like the way things are displayed, you can inherit the Manual class to redefine your way.

```javascript
const CLI = require("@jkhong/cli-js");

[...]

class CustomManual extends CLI.Manual {
// methods that can be overriden
    // about(cliOptions) { }
    // getSwitchPrefix() {}
    // printHelp(cliOptions) {}
    // displaySectionHeader(section) {}
    // displayAction(action) {}
}

try {
    const cli = new CLI.Interface();
    cli.setManual(new CustomManual(manualContent));
    cli.launch();
}
catch (e) {
    console.log(e);
}
```

## The cli

You can customize the way cli displays error by inheriting methods

```javascript
class CustomCli extends CLI.Interface {
// methods that can be overriden
    displayHeader() {
        // display what you want here
    }
    manageException(e) {
        // display what you want here
    }
}
```

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