1.0.0-rc.0 • Published 2 years ago

@bizhermit/cli-utils v1.0.0-rc.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

CLI SDK

CLI SDK.

Install

npm i @bizhermit/cli-utils

Example

import Cli, { getKeyArgs } from "@bizhermit/cli-utils";

const func = async () => {
  const command = Cli.getArg();
  const dest = Cli.getKeyArg("-d");
  const excludes = getKeyArgs("-excludes");
  const text = await Cli.rl("input > ");
  await Cli.wl(text);
};
func();

Methods

  • rl(message?: string) => Promise\
    read input text line.

    const text = await rl("input > ");
    # console
    input > _
  • wl(message?: string) => void
    write text for console.

    wl("hoge");
    # console
    hoge
  • getArg(index?: number) => string | undefined
    get arg value.

    node cli hoge fuga piyo
    console.log(getArg()); // => "hoge"
    console.log(getArg(2)); // => "piyo"
    console.log(getArg(5)); // => undefined
  • getArgs() => Array\
    get arg values.

    node cli hoge fuga piyo
    console.log(getArgs()); // => ["hoge", "fuga", "piyo"]
  • hasKeyArg(key: string, subKey?: string) => boolean
    return has key arg.

    node cli --hoge
    console.log(hasKeyArg("--hoge")); // => true
    console.log(hasKeyArg("--fuga")); // => false
  • getKeyArg(key: string, subKey?: string) => string | undefined
    return arg value.

    node cli -hoge 1 2 3 4 -fuga 5 -piyo -foo
    console.log(getKeyArg("-hoge")); // => "1"
    console.log(getKeyArg("-fuga")); // => "5"
    console.log(getKeyArg("-piyo")); // => undefined
    console.log(getKeyArg("-none")); // => undefined
  • getKeyArgs(key: string, subKey?: string) => Array\
    return arg values. continue get value until value start with hyphen or end.

    node cli -hoge 1 2 3 -fuga 4 -piyo -foo
    console.log(getKeyArgs("-hoge")); // => ["1", "2", "3"]
    console.log(getKeyArgs("-fuga")); // => ["4"]
    console.log(getKeyArgs("-piyo")); // => []
    console.log(getKeyArgs("-foo"));  // => []
    console.log(getKeyArgs("-none")); // => []