0.0.12 • Published 1 year ago

lmcmd v0.0.12

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

@lmssee/command

This is a project for terminal interaction, serving the cil class

language

install

npm install   lmcmd  --save

use

import Command from "lmcmd";

Command section

The comprehensive part is to put Args, selection, and question together

For specific usage, please refer to their own instructions section

Args section (get user start program params)

Args can obtain the parameters passed in by the user when starting the program

Analyzing user input parameters

will only start working after calling run, and all bind must be completed before run

Please note that the execution is sequential, and once the run operation is completed, the bind operation cannot be executed. If you insist on doing so, users may see strange information that was originally meant to remind you .

During the execution process, you can refer to the 'state' value to view. When the user is only referring to the version number or printing help, the 'state. code' will be 4, and 'state. overText' will be returned to indicate whether they are referring to the version number or printing help. It is not recommended to execute other commands when 'state. code' is 4. You can also print some other fun ones

  • commandName argName
  • commandName argName value
  • commandName argName optionName
  • commandName argName optionName value

Example:

If your command name is gig , You added parameters:

Please avoid abbreviations as much as possible hv

When you have multiple configuration items, you can use an array to group the configuration items that comply with the rules

  • Simplified example
    import { Args } from "lmcmd";
    const command: Args = new Args();
    command.bind("init <-i> (Initialize configuration file)").run();
  • Simple configuration example
    import { Args } from "lmcmd";
    const command: Args = new Args();
    command
      .bind({
        name: "init",
        abbr: "-i",
        info: "Initialize configuration file",
      })
      .run();
  • Example of carrying sub item configuration

    import { Args } from "lmcmd";
    const command: Args = new Args();
    command.bind({
      name: "init",
      abbr: "-i",
      info: "Initialize configuration file",
      options: [
        "ts <-t> (Initialize a `ts` configuration file)",
        "js <-j> (Initialize a `js` configuration file)",
        "json <-o> (Initialize a `json` configuration file)",
      ],
    });
    command.run(); // Users can use `gig init -o`
  • Example of carrying detailed configuration of sub items

    import { Args } from "lmcmd";
    const command: Args = new Args();
    command.bind({
      name: "init",
      abbr: "-i",
      info: "Initialize configuration file",
      options: [
        {
          name: "ts",
          abbr: "-t",
          info: "Initialize a `ts` configuration file",
        },
        {
          name: "js",
          abbr: "-j",
          info: "Initialize a `js` configuration file",
        },
        {
          name: "json",
          abbr: "-o",
          info: "Initialize a `json` configuration file",
        },
      ],
    });
    command.run(); // Users can use `gig init -o`
    • Finally, you can use getArgs to obtain the user's actual value input
    ... // other code




     /**
      *   Obtain processed user input parameters
      *
      *  This mode preserves user input as much as possible, but also discards some unrecognized inputs
      * */
    command.getArgs;
    /**
     *  Obtain the Object form of the processed user input parameters
     *
     *  This mode is more suitable for configuring files
     *
     * **_In this mode, `subOptions` will overwrite the superior's `value`_**
     *
     * */
    command.getArgs.map;
     /**
      *   Obtain a simple form of the processed user input parameters
      *
      *  This mode is suitable for simple commands, only checking if the command has
      * */
    command.getArgs.only;

      /**
   *
   *   Is it empty? Check if the user has not entered command parameters
   */
    command.getArgs.$isVoid;
    /**
     *
     * User's original input parameters
     */
    command.getArgs.$original;

Get current status

/**
 * is over ? you will get  a  boolean value
 *  although ,it over
 *  you can do other thing  if you want , you can get `state`  for what over
 *
 */
command.isEnd;
/**
 * If you have nothing else to do after
 *  the user uses the help document or
 * printed version information,
 *
 * you can use the `end`
 *
 */
command.isEnd.end;
command.state; //  state
command.state.code; // state code
command.state.overText; //    "version" | "help" | undefined;

question section (Q&A mode)

Question 'is a question and answer mode that can be used to ask users questions or make simple choices. After referencing this function, use it where needed _A function waiting for user input. Because it needs to wait, it is asynchronous, and when using it,wait` should be used_

Example

The simplest use :

import { question } from "lmcmd";
const result = await question("What do you want for lunch");

Using custom configurations can provide users with a better experience.

import { question } from "lmcmd";
const result = await question({
  text: "What do you want for lunch",
  tip: "Hamburg or Italian pasta",
  type: "text",
});

You can also configure 'tip' as an array and configure Q&A as a simple selection.At this point, users can only choose from the values provided by 'tip' Only suitable for simple selection, such as' yes' or 'no' or 'male' or 'female' options with more words, it is recommended to use selection (# selection - section - selection mode -)

import { question } from "lmcmd";

const result = await question({
  text: "What do you want for lunch", // Required parameters
  tip: ["Hamburg ", " Italian pasta"], // Optional parameter, enter selection mode when it is an array
  type: "text", //A type selection that supports `text` and `password`,Optional parameter, default : `text``
  private: false, // Overwrite after input,Optional parameter,,default: `false`
  resultText: "Okay, then let's go eat", // Optional parameter,of  result display
});

Multiple questions can also be provided at once, just place them in an array (array and object patterns can be mixed and matched)

import { question } from "lmcmd";

const result = await question([
  {
    text: "What do you want for lunch",
    tip: ["Hamburg ", "Italian pasta"],
    resultText: "Okay, then let's go eat",
  },
  {
    text: "What`s your favorite dessert",
    private: true,
  },
  "Where to play after dinner",
]);

selection section (Select mode)

After referencing this function, use it where needed A function waiting for user input. Because it needs to wait, it is asynchronous, and when using it, wait should be used

Example

The simplest use :

import  { selection } from ""lmcmd;
console.log("What do you want for lunch");
const result = await selection([
    "Hamburg",
    "Italian pasta",
    "steak",
    "pizza",
    "chafing dish",
]);

Full configuration :

import  { selection } from ""lmcmd;

const result = await selection({
    showInfo: true,
    info: "What do you want for lunch?",
    data: [
        "Hamburg",
        "Italian pasta",
        "steak",
        "pizza",
        "chafing dish",

    ],
    showPreview: true,
    preview: "currently want to eat"
     resultText: "Okay, then let's go eat "
});

If you don't want to display the issue and preview, you can use the pattern of the incoming object for custom configuration

cursor section

You can use cursor to manipulate the cursor position:

MethodSchematicParameters
cursorHidecursor hide--
cursorShowcursor show--
cursorPositionSaveStore cursor position--
cursorPositionRestoreRestores cursor position--
cursorMoveUpcursor UpnumberOfUpwardMoves offset, default to 1
cursorMoveDowncursor DownnumberOfMovesDown offset, default to 1
cursorMoveLeftcursor LeftnumberOfLeftShifts offset, default to 1
cursorMoveRightcursor RightnumberOfRightShifts offset, default to 1

readInput section

A function waiting for user input. Because it needs to wait, it is asynchronous, and when using it, wait should be used

import { readInput } from "lmcmd";

const callBackFunction = (keyValue: string | undefined, key: any) => {
  if (key.name && key.name == "return") return true;
  else
    return console.log(
      `Try a different key, this key (${keyValue}) do not execute exit`
    );
};

If you have any questions, you can send a email or push twitter or directly submit question

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago