Skip to content

wasabi-io/command-helper

Repository files navigation

command-helper

npm package Build Status codecov

Motivation

  • Command Helper is used to get result from the given commands and provides to remove current command from the given command array if needed.
  • You can use standart readers (boolean, string, array ) to read command and also you can define custom readers to read commands.

Template Format

Main Template

  • name : name of your bin js file.
  • version: version of your library.
  • commands: command reference to find your command template.
  • options: holds each command template options.
{
  "name": "command-helper",
  "version": "1.0.0",
  "commands": {
    ...
  },
  "options": {
    ...
  }
}

Command Template

  • Main Template -> options[key]: Define command template in ${Main Template}.options{variable name}
    • reader: Reader is read command and find value of command in the given arguments.
      • name: Its shown on help board.
      • main: it is reader function to read the command and find its values.
      • related: called by main reader if exist.
    • defaultValue: it is default value for the command.
    • remove: Indicates if the command read in the given arguments then remove all things about the command from the given arguments.
    • infinity: read command recursively from the given arguments.
    • description: Its shown on help board.
    • usage: Its used on help board.
      • usage = 0 then --command
      • usage = 1 then --command value
      • usage = -1 then --files value1, [value2, ...]
      • else shows usage if exist.
  • Short Template: It used when the template file keeps as json.
{
    "reader": "Boolean",
    "defaultValue": false,
    "description": "Renderer Description",
    "usage": 0
}
  • Full Template: If you need to set custom readers then you can use full template.
{
    "reader": {
        "name": "Array<JsonFile>",
        "main": Readers.Array,
        "related": Readers.JsonFile
      },
      "defaultValue": [],
      "remove": true,
      "infinity": false,
      "description": "Json Files Description",
      "usage": -1
}

Standart Readers

  • Boolean [...,"--command",....]

  • String -> [...,"--command", "value",....] or [...,"--command=value",....]

  • Array (Array) ["--command", "value1", "value2", "value3"]

  • TextFile -> [...,"--command", "path",....] or [...,"--command=path",....]

  • JsonFile -> [...,"--command", "path",....] or [...,"--command=path",....]

  • Array [...,"--command", "path1", "path2", "path3",....]

  • Array [...,"--command", "path1", "path2", "path3",....]

  • You can override standard readers when you call Helper file.

import {  } "api/Api";
let customReaders = {
     Boolean: (state: CommandState, opts) => {
         if (!has(opts[state.name]) && state.args.length > state.i) {
             let def = state.template.defaultValue;
             opts[state.name] = has(def) ? !def : true;
             return true;
         }
         return false;
     },
     NewType: (state: CommandState, opts) => {
         if (!has(opts[state.name]) && state.args.length > state.i) {
             let def = state.template.defaultValue;
             opts[state.name] = has(def) ? !def : true;
             return true;
         }
         return false;
     } 
}

let helper = new Helper(template, customerReaders)

Examples

  • Json Template Example
let templateJson = {
  "name": "command-helper",
  "version": "1.0.0",
  "commands": {
    "--renderer": "renderer",
    "--interactive": "interactive",
    "--base-url": "baseUrl",
    "--root-list": "rootList",
    "--opts": "opts",
    "--tdd-json": "tddJson",
    "--files": "files",
    "--another-files": "anotherFiles",
    "--json-files": "jsonFiles"
  },
  "options": {
    "renderer": {
      "reader": "Boolean",
      "defaultValue": false,
      "description": "Renderer Description",
      "usage": 0
    },
    "interactive": {
      "reader": "Boolean",
      "defaultValue": false,
      "description": "Interactive Description",
      "usage": 0
    },
    "baseUrl": {
      "reader": "String",
      "defaultValue": "",
      "description": "Base Url Description",
      "usage": 1
    },
    "rootList": {
      "reader": "Array",
      "defaultValue": [],
      "description": "Root List Description",
      "usage": -1
    },
    "opts": {
      "reader": "TextFile",
      "defaultValue": "",
      "description": "Opts Description",
      "usage": 1
    },
    "tddJson": {
      "reader": "JsonFile",
      "defaultValue": {},
      "description": "Tdd Json Description",
      "usage": 1
    },
    "files": {
      "reader": "Array<TextFile>",
      "defaultValue": [],
      "description": "Files Description",
      "usage": -1
    },
    "jsonFiles": {
      "reader": "Array<JsonFile>",
      "defaultValue": [],
      "description": "Json Files Description",
      "usage": -1
    }
  }
}
let helper = new Helper(template);
let result = helper.parse(process.argv);

Contribute

  • Install
$ git clone https://github.com/wasabi-io/command-helper.git`
$ cd command-helper
$ npm install
  • Test
$ npm test
  • Coverage
$ npm run coverage
  • Export Docs
$ npm run docs
  • Build Code as javascript (common-js)
$ npm build

Releases

No releases published

Packages

No packages published