4.0.0 • Published 5 years ago
@writ/command v4.0.0
@writ/command
Mime, Command parameter parsing for building cli applications. 中文
Table of Contents
Features
- elegant command line parameter parsing
- configure the build command line application
Install
install node.js, Now, install and use this project
npm install @writ/commandUsage
Project Structure
├─ example/ `usage examples` ├─ index.js `entry points` ├─ test/ `test code` ├─ .gitignore `git ignore` ├─ .eslintrc.js `eslint format config` ├─ license `agreement that` ├─ package.json `table of modules with npm` └─ README.md `description`Usage You can see my example here
- Configuration using
functiontypes, You can do some work in the functions
#! /usr/bin/env node const Command = require('@writ/command'); new Command(function(command) { return require('../.clirc'); }).start();- Configuration using
objecttypes
#! /usr/bin/env node const Command = require('@writ/command'); new Command({ root: '.', order: { help: { param: [], alias: ['h', '-h'] }, version: { alias: [ 'v', 'V', '-v', '--version' ] } }, action: { help: require('../src/help'), version: require('../src/version') } }).start();- Configuration using
stringtypes
#! /usr/bin/env node const Command = require('../../src'); new Command('../example/.clirc.js').start();- Configuration using
Options example
options.rootstring yourcli-approot diroptions.actionstring|object command handler, it is a dirname or an objectoptions.orderobject command detail info// for example: module.exports = { root: '.', action: { example(param) { if (param.all) { process.stdout.write(`All: ${param.all.join(' ')}\n`); } if (param.bail) { process.stdout.write(`Bail: ${param.bail.join(' ')}\n`); } if (param.comment) { process.stdout.write(`Comment: ${param.comment.join(' ')}\n`); } } }, order: { // Declare the 'example` subcommand example: { // example's alias alias: [ 'ex', '-e' ], // command parameters, the rule is the '--' beginning refers to the full name of the parameter, the '-' beginning refers to the corresponding abbreviation // in a program, or will be converted to the full name, such as the '-a' convert 'param.all = []' param: [ '--all -a', '--bail -b', '--comment -c' ] } } }The above configuration implements a child command
<main-command-name> example param, a command onlyaliasandparamtwo properties, and are currently only supports arrays,paramstatement for eachparamter in the code:--<name> -<alias>, all will be used in the correspondingaction, referred to as "only to belong to convenient
API intro In each
action,thisalways points to yourCommandinstances, So, you can useCommand'smethods and properties in eachaction.rootstring yourcli-app'sroot diractRootstring command handler file diractionobject commamd handlersordersobject command setruntimeobject currently executing command's infomation- start(argv<array>) start loader
- whoami(enter<string>) find currently command's name
- parse(param<array|*>) parse currently command's options
- invalid() print invaild infomation
Change log
- Founded in Wed, 24 Oct 2018 01:38:45 GMT
- Add the test case, Mon, 28 Jan 2019 05:01:27 GMT
- Add usage, Sat, 16 Feb 2019 04:07:42 GMT