# Commander.js
[![Build Status](https://github.com/tj/commander.js/workflows/build/badge.svg)](https://github.com/tj/commander.js/actions?query=workflow%3A%22build%22)
[![NPM Version](http://img.shields.io/npm/v/commander.svg?style=flat)](https://www.npmjs.org/package/commander)
[![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://npmcharts.com/compare/commander?minimal=true)
[![Install Size](https://packagephobia.now.sh/badge?p=commander)](https://packagephobia.now.sh/result?p=commander)
The complete solution for [node.js](http://nodejs.org) command-line interfaces.
Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
- [Commander.js](#commanderjs)
- [Installation](#installation)
- [Quick Start](#quick-start)
- [Declaring _program_ variable](#declaring-program-variable)
- [Options](#options)
- [Common option types, boolean and value](#common-option-types-boolean-and-value)
- [Default option value](#default-option-value)
- [Other option types, negatable boolean and boolean|value](#other-option-types-negatable-boolean-and-booleanvalue)
- [Required option](#required-option)
- [Variadic option](#variadic-option)
- [Version option](#version-option)
- [More configuration](#more-configuration)
- [Custom option processing](#custom-option-processing)
- [Commands](#commands)
- [Command-arguments](#command-arguments)
- [More configuration](#more-configuration-1)
- [Custom argument processing](#custom-argument-processing)
- [Action handler](#action-handler)
- [Stand-alone executable (sub)commands](#stand-alone-executable-subcommands)
- [Life cycle hooks](#life-cycle-hooks)
- [Automated help](#automated-help)
- [Custom help](#custom-help)
- [Display help after errors](#display-help-after-errors)
- [Display help from code](#display-help-from-code)
- [.name](#name)
- [.usage](#usage)
- [.description and .summary](#description-and-summary)
- [.helpOption(flags, description)](#helpoptionflags-description)
- [.addHelpCommand()](#addhelpcommand)
- [More configuration](#more-configuration-2)
- [Custom event listeners](#custom-event-listeners)
- [Bits and pieces](#bits-and-pieces)
- [.parse() and .parseAsync()](#parse-and-parseasync)
- [Parsing Configuration](#parsing-configuration)
- [Legacy options as properties](#legacy-options-as-properties)
- [TypeScript](#typescript)
- [createCommand()](#createcommand)
- [Node options such as `--harmony`](#node-options-such-as---harmony)
- [Debugging stand-alone executable subcommands](#debugging-stand-alone-executable-subcommands)
- [Display error](#display-error)
- [Override exit and output handling](#override-exit-and-output-handling)
- [Additional documentation](#additional-documentation)
- [Support](#support)
- [Commander for enterprise](#commander-for-enterprise)
For information about terms used in this document see: [terminology](./docs/terminology.md)
## Installation
```sh
npm install commander
```
## Quick Start
You write code to describe your command line interface.
Commander looks after parsing the arguments into options and command-arguments,
displays usage errors for problems, and implements a help system.
Commander is strict and displays an error for unrecognised options.
The two most used option types are a boolean option, and an option which takes its value from the following argument.
Example file: [split.js](./examples/split.js)
```js
const { program } = require('commander');
program
.option('--first')
.option('-s, --separator <char>');
program.parse();
const options = program.opts();
const limit = options.first ? 1 : undefined;
console.log(program.args[0].split(options.separator, limit));
```
```console
$ node split.js -s / --fits a/b/c
error: unknown option '--fits'
(Did you mean --first?)
$ node split.js -s / --first a/b/c
[ 'a' ]
```
Here is a more complete program using a subcommand and with descriptions for the help. In a multi-command program, you have an action handler for each command (or stand-alone executables for the commands).
Example file: [string-util.js](./examples/string-util.js)
```js
const { Command } = require('commander');
const program = new Command();
program
.name('string-util')
.description('CLI to some JavaScript string utilities')
.version('0.8.0');
program.command('split')
.description('Split a string into substrings and display as an array')
.argument('<string>', 'string to split')
.option('--first', 'display just the first substring')
.option('-s, --separator <char>', 'separator character', ',')
.action((str, options) => {
const limit = options.first ? 1 : undefined;
console.log(str.split(options.separator, limit));
});
program.parse();
```
```console
$ node string-util.js help split
Usage: string-util split [options] <string>
Split a string into substrings and display as an array.
Arguments:
string string to split
Options:
--first display just the first substring
-s, --separator <char> separator character (default: ",")
-h, --help display help for command
$ node string-util.js split --separator=/ a/b/c
[ 'a', 'b', 'c' ]
```
More samples can be found in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.
## Declaring _program_ variable
Commander exports a global object which is convenient for quick programs.
This is used in the examples in this README for brevity.
```js
// CommonJS (.cjs)
const { program } = require('commander');
```
For larger programs which may use commander in multiple ways, including unit testing, it is better to create a local Command object to use.
```js
// CommonJS (.cjs)
const { Command } = require('commander');
const program = new Command();
```
```js
// ECMAScript (.mjs)
import { Command } from 'commander';
const program = new Command();
```
```ts
// TypeScript (.ts)
import { Command } from 'commander';
const program = new Command();
```
## Options
Options are defined with the `.option()` method, also serving as documentation for the options. Each option can have a short flag (single character) and a long name, separated by a comma or space or vertical bar ('|').
The parsed options can be accessed by calling `.opts()` on a `Command` object, and are passed to the action handler.
Multi-word options such as "--template-engine" are camel-cased, becoming `program.opts().templateEngine` etc.
An option and its option-argument can be separated by a space, or combined into the same argument. The option-argument can follow the short option directly or follow an `=` for a long option.
```sh
serve -p 80
serve -p80
serve --port 80
serve --port=80
```
You can use `--` to indicate the end of the options, and any remaining arguments will be used without being interpreted.
By default options on the command line are not positional, and can be specified before or after other arguments.
There are additional related routines for when `.opts()` is not enough:
- `.optsWithGlobals()` returns merged local and global option values
- `.getOptionValue()` and `.setOptionValue()` work with a single option value
- `.getOptionValueSource()` and `.setOptionValueWithSource()` include where the option value came from
### Common option types, boolean and value
The two most used option types are a boolean option, and an option which takes its value
from the following argument (declared with angle brackets like `--expect <value>`). Both are `undefined` unless specified on command line.
Example file: [options-common.js](./examples/options-common.js)
```js
program
.option('-d, --debug', 'output extra debugging')
.option('-s, --small', 'small pizza size')
.option('-p, --pizza-type <type>', 'flavour of pizza');
program.parse(process.argv);
const options = program.opts();
if (options.debug) console.log(options);
console.log
没有合适的资源?快使用搜索试试~ 我知道了~
练习项目配套的node后端代码
共505个文件
js:199个
md:111个
json:81个
需积分: 1 0 下载量 64 浏览量
2023-02-17
15:24:56
上传
评论
收藏 1.49MB ZIP 举报
温馨提示
练习项目配套的node后端代码
资源推荐
资源详情
资源评论
收起资源包目录
练习项目配套的node后端代码 (505个子文件)
random.cmd 322B
mime.cmd 316B
.editorconfig 569B
.editorconfig 286B
.editorconfig 260B
.eslintignore 10B
.eslintignore 10B
.eslintrc 1KB
.eslintrc 1KB
.eslintrc 603B
.eslintrc 247B
.eslintrc 231B
.eslintrc 180B
.eslintrc 176B
.eslintrc 172B
.eslintrc 164B
.gitattributes 12B
test.mock.html 2KB
noop.html 77B
mock.js 266KB
mock-min.js 136KB
address_dict.js 110KB
command.js 68KB
qs.js 68KB
parse.js 35KB
stringify.js 34KB
sbcs-data-generated.js 31KB
response.js 27KB
parser.js 26KB
index.js 23KB
test.mock.spec.dtd.js 21KB
dbcs-codec.js 21KB
handler.js 20KB
ipaddr.js 19KB
index.js 18KB
valid.js 16KB
test.mock.random.js 16KB
test.mock.request.js 15KB
tests.js 15KB
index.js 15KB
xhr.js 15KB
application.js 14KB
help.js 14KB
index.js 13KB
request.js 12KB
handler.js 12KB
index.js 11KB
index.js 10KB
stringify.js 10KB
ipaddr.min.js 10KB
parse.js 9KB
utf7.js 9KB
image.js 9KB
index.js 9KB
GetIntrinsic.js 9KB
extend-node.js 8KB
option.js 8KB
dbcs-data.js 8KB
test.mock.spec.dpd.js 8KB
test.mock.schema.js 7KB
values.js 7KB
index.js 7KB
utils.js 7KB
index.js 7KB
indent-option.js 6KB
index.js 6KB
index.js 6KB
test.mock.valid.js 6KB
internal.js 6KB
node.js 6KB
index.js 6KB
utils.js 6KB
urlencoded.js 6KB
index.js 5KB
mediaType.js 5KB
index.js 5KB
index.js 5KB
color.js 5KB
valid.js 5KB
index.js 5KB
utils.js 5KB
text.js 5KB
json.js 5KB
utf16.js 5KB
index.js 5KB
gulpfile.js 5KB
inspect.js 5KB
browser.js 5KB
sbcs-data.js 5KB
index.js 4KB
index.js 4KB
debug.js 4KB
read.js 4KB
route.js 4KB
index.js 4KB
basic.js 4KB
date.js 4KB
index.js 4KB
web.js 4KB
index.js 4KB
共 505 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
COW码
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于微信小程序+mysql的答题系统的设计与实现+全部资料+详细文档(毕业设计).zip
- H5U的PLC固件,用于EthernetIP通讯时升级固件使用
- Jumpserver堡垒机快速入门视频.zip
- 基于springboot+mysql+redis实现个性化书籍推荐和相似帖子推荐的社区图书馆+论坛+失物招领平台
- 软考中级软件设计师 法律法规通关笔记
- 基于java+sqlserver实现的员工培训系统
- java毕业设计基于Springboot的贫困生资助系统源码+使用文档+全部资料(优秀项目).zip
- 基于随机森林和袋装法对PM2.5浓度进行回归预测
- 静态路由实验.docx
- Rust语言探索之旅:从入门到实践,构建安全高效的系统级应用-文章博客-markdown材料.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功