Skip to content

Repository files navigation

@axiosleo/cli-tool

English | 简体中文

NPM versionnpm downloadnode versionCI Build StatusCodecovLicenseFOSSA 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');constapp=newApp({name: 'cli-tool',// cli app command name, requiredversion: '1.0.0',// cli app version, requireddesc: 'cli app description',commands_dir: '/path/to/commands/dir/',// will auto load command filescommands_sort: ['help', ... ],commands_group: {'group description': ['command_name', ...],}});app.start();

Run single command

  • register with command class
constCommandExample=require("/path/to/your/command/file");app.register(CommandExample);app.exec("<command-name>");
  • register with command object
constCommandExample=require("/path/to/your/command/file");constcommand=newCommandExample();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

constpath=require("path");app.locale({dir: path.join(__dirname,"../locales"),// /path/to/app/locales/dirsets: ["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");classCommandExampleextendsCommand{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);}asyncexec(args,options,argList,app){// do something in here// get arg&option by nameconstarg1=args.argName;constoption1=options.optionName;// get arg by indexconstindex=0;constarg2=argList[index];// ask for answerconstanswer=awaitthis.ask("Please input your answer");// ask for confirm, default value is 'false'constconfirm=awaitthis.confirm("Confirm do this now?",false);// select actionconstaction=awaitthis.select("Select an action",["info","update"]);// print tableconstrows=[["Bob",2]];consthead=["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

Resources

Stars

37 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages