GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - AxiosLeo/node-cli: Design for quickly developing CLI applications using Node.js

BranchesTags
Open more actions menu

Latest commit

 

History

534 Commits
534 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
.vscode
.vscode
 
 
bin
bin
 
 
commands
commands
 
 
locales
locales
 
 
src
src
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.npmignore
.npmignore
 
 
LICENSE
LICENSE
 
 
README-CN.md
README-CN.md
 
 
README.md
README.md
 
 
eslint.config.mjs
eslint.config.mjs
 
 
index.d.ts
index.d.ts
 
 
index.js
index.js
 
 
nyc.config.js
nyc.config.js
 
 
package.json
package.json
 
 

Repository files navigation

@axiosleo/cli-tool

English | 简体中文

NPM version npm download node version CI Build Status Codecov License FOSSA Status

Design for quickly developing CLI applications using Node.js

See detail usage from wiki

Installation

npm install @axiosleo/cli-tool

Quickly initialize application

npm install @axiosleo/cli-tool -g

cli-tool init <app-name>

# make command file
cli-tool make <command-name> <commands-dir-path>
# for example
cli-tool make test ./commands/ # will generate command file on ./commands/test.js

# run command js script
cli-tool exec ./command/test.js

Usage

Start application

const { App } = require('@axiosleo/cli-tool');
const app = new App({
  name: 'cli-tool',      // cli app command name, required
  version: '1.0.0',      // cli app version, required
  desc: 'cli app description',
  commands_dir: '/path/to/commands/dir/', // will auto load command files
  commands_sort: ['help', ... ],
  commands_group: {
    'group description': ['command_name', ...],
  }
});
app.start();

Run single command

  • register with command class
const CommandExample = require("/path/to/your/command/file");
app.register(CommandExample);

app.exec("<command-name>");
  • register with command object
const CommandExample = require("/path/to/your/command/file");
const command = new CommandExample();
app.register(command);

app.exec("<command-name>");
  • register with command file path
app.register("/path/to/your/command/file");

app.exec("<command-name>");

Use locales

The "desc" of CLI Application and Command will be automatically translated by using the locales json file.

locales example json file : locales

see detail from locales wiki

const path = require("path");
app.locale({
  dir: path.join(__dirname, "../locales"), // /path/to/app/locales/dir
  sets: ["en-US", "zh-CN"], // cannot be empty, the first set as default.
});
app.start(); // set locale before start app

Command Class Example

"use strict";

const { Command } = require("@axiosleo/cli-tool");

class CommandExample extends Command {
  constructor() {
    super({
      name: "command-name",
      desc: "command desc",
      alias: ["command-alia1", "command-alia2"],
    });

    /**
     * add argument of current command
     * @param name argument name
     * @param desc argument description
     * @param mode argument mode : required | optional
     * @param default_value only supported on optional mode
     */
    this.addArgument("arg-name", "desc", "required", null);

    /**
     * add option of current command
     * @param name option name
     * @param short option short name
     * @param desc option description
     * @param mode option mode : required | optional
     * @param default_value only supported on optional mode
     */
    this.addOption("test", "t", "desc", "required", null);
  }

  async exec(args, options, argList, app) {
    // do something in here

    // get arg&option by name
    const arg1 = args.argName;
    const option1 = options.optionName;

    // get arg by index
    const index = 0;
    const arg2 = argList[index];

    // ask for answer
    const answer = await this.ask("Please input your answer");

    // ask for confirm, default value is 'false'
    const confirm = await this.confirm("Confirm do this now?", false);

    // select action
    const action = await this.select("Select an action", ["info", "update"]);

    // print table
    const rows = [["Bob", 2]];
    const head = ["Name", "Score"];
    this.table(rows, head);
  }
}

module.exports = CommandExample;

License

This project is open-sourced software licensed under MIT.

FOSSA Status

About

Design for quickly developing CLI applications using Node.js

Topics

clicli-toolnodenode-clinodejs

Resources

Readme
MIT license
Activity

Stars

37 stars

Watchers

1 watching

Forks

3 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.