Giter VIP home page Giter VIP logo

tscliparser's Introduction

tscliparser

CLI Parser library in TypeScript.

Get Started

Install

npm install tscliparser

Try using tscliparser

Sample

Construct CLI

/*
(1) Import CliParser and types from tscliparser.
*/
import {
    CliParams,
    OptionAdvanced,
    ValueAdvanced,
    HelpOption,
} from "tscliparser/types/typesTsCliParser";
import { CliParser } from "tscliparser/tscliparser";

/*
(2) Define CLI arguments with interface(extends CliParams).
If you want to test, you have to export interface(extends CliParams).
*/
export interface ParamsDisplayHtml extends CliParams {
    host: { // User add.
        data: string;
        advanced: OptionAdvanced;
    };
    port: { // User add.
        data: number;
        advanced: OptionAdvanced;
    };
    html: { // User add.
        data: string;
        advanced: ValueAdvanced;
    };
    help: HelpOption; // Required.
    test: { // Required.
        data: false;
        advanced: {
            required: false;
            short: "";
        };
    };
}

/*
(3) Create instance of CliParser with interface(extends CliParams) and default data in each argument.
If you want to test, you have to export cliparser(instance of CliParser).
*/
export const cliparser = new CliParser<ParamsDisplayHtml>({
    host: {
        data: "127.0.0.1",
        advanced: {
            required: false,
            short: "h",
        },
    },
    port: {
        data: 8765,
        advanced: {
            required: false,
            short: "p",
        },
    },
    html: {
        data: "index.html",
        advanced: {
            required: true,
            location: 1,
        },
    },
    test: { // You have to set constant value below.
        data: false,
        advanced: {
            required: false,
            short: "",
        },
    },
    help: { // Not allowed to be empty in 'usage'.
        usage: `[Usage] node displayHtml.js [-h|--host HOST] [-p|--port PORT] [--help] html
Options:
-h|--host HOST: Host that will be used to start HTTP Server. (default: 127.0.0.1)
-p|--port PORT: Port that will be used to start HTTP Server. (default: 8765)
--help        : Display help.
html          : HTML file you want to display with HTTP Server.`,
        description: "This CLI shows the preview of HTML.",
    },
});

if (require.main === module) {
    /*
    (4) Parse CLI arguments with cliparser(instance of CliParser).
    */
    cliparser.parse(process.argv);

    /*
    (5) Use parameters which cliparser stores...
    */
    const cliParams: ParamsDisplayHtml = cliparser.getParams();
    ...
}

Test

import {
    testTsCliParser
} from "tscliparser/test/testTsCliParser";
import {
    ParamsDisplayHtml,
    cliparser
} from "~~/sample/main/displayHtml";

describe("test CLI displayHtml", () => {
    testTsCliParser<ParamsDisplayHtml>(
        {
            cliparser: cliparser,
            cmd: "node webpack_build/dist/displayhtml_dev.js -p 333 -h 1.1.1.1 test.html"
        },
        {
            params: {
                port: 333,
                host: "1.1.1.1",
                html: "test.html"
            },
        }
    );
});

CLI error sample

root:/workspace# node dist/sample/main/displayHtml.js -a 3
e: Not found argument from short argument 'a'.

root:/workspace# node dist/sample/main/displayHtml.js --aaa
e: CliParser cannot find argument 'aaa'.

root:/workspace# node dist/sample/main/displayHtml.js -p 44a
TypeError: Invalid type in argument 'port'(expected number, but string).

root:/workspace# node dist/sample/main/displayHtml.js -h -t
e: Detected danger in consecutive arguments 'host' and '-t'. If you want set strings start with '-', you should enclose the string with quotes(single or double).

License

Copyright © 2021 by KagenoMoheji. Released under the MIT License.

tscliparser's People

Contributors

kagenomoheji avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.