进阶 oclif.io 2026-10-18 02:37:03 · 26 阅读
第14章 Command 命令参数详解
# 第14章 Command Arguments
命令参数是按位置传给命令的参数。比如执行 `mycli arg1 arg2` 时,参数可以这样声明:
import {Args, Command} from '@oclif/core'
export class MyCLI extends Command {
static args = {
firstArg: Args.string(),
secondArg: Args.string(),
}
async run() {
// can get args as an object
const {args} = await this.parse(MyCLI)
this.log(`running my command with args: ${args.firstArg}, ${args.secondArg}`)
// can also get the args as an array
const {argv} = await this.parse(MyCLI)
this.log(`running my command with args: ${argv[0]}, ${argv[1]}`)
}
}
参数支持以下选项: static args = {
firstArg: Args.string(
{
name: 'file', // name of arg to show in help and reference with args[name]
required: false, // make the arg required with `required: true`
description: 'output file', // help description
hidden: true, // hide this arg from help
parse: async input => 'output', // instead of the user input, return a different value
default: 'world', // default value if no arg input. Can also be an async function.
defaultHelp: 'a dynamic value' // dynamic default value to show in help output (e.g. current working directory). Can be an async function that returns a string or undefined
options: ['a', 'b'], // only allow input to be from a discrete set
ignoreStdin: false, // set to true to ignore any value provided by stdin
noCacheDefault: false, // if true, the value returned by defaultHelp will not be cached in the oclif.manifest.json.
multiple: false // if true, this argument will support multiple inputs and parse them as an array
}
),
}
Args 导出的参数类型包括: string integer boolean url file directory custom 对于可变长度的参数,使用 multiple: true 属性。注意使用时,该参数之前和之后的所有参数都必须设置为 required: true。 import {Args, Command} from '@oclif/core'
export class MyCLI extends Command {
static args = {
arg1: Args.string({required: true}),
arg2: Args.string({multiple: true, required: true}),
arg3: Args.string({required: true})
}
async run() {
const {args} = await this.parse(MyCLI)
this.log(`arg 1 is ${args.arg1}`) // args.arg1 is a string
this.log(`arg 2 is ${args.arg2.join(',')}`) // args.arg2 is a string[]
this.log(`arg3 is ${args.arg3}`) // args.arg3 is a string
}
}
参数也可通过标准输入(stdin)传入。这在结合 CLI 命令编写脚本时尤为方便。示例如下: $ echo arg1 | mycli
running my command with args: arg1
若需禁用某一参数的此行为,可在其定义中将 ignoreStdin 属性设为 true。
export class MyCLI extends Command {
static args = {
firstArg: Args.string(),
secondArg: Args.string(),
}
async run() {
// can get args as an object
const {args} = await this.parse(MyCLI)
this.log(`running my command with args: ${args.firstArg}, ${args.secondArg}`)
// can also get the args as an array
const {argv} = await this.parse(MyCLI)
this.log(`running my command with args: ${argv[0]}, ${argv[1]}`)
}
}
参数支持以下选项: static args = {
firstArg: Args.string(
{
name: 'file', // name of arg to show in help and reference with args[name]
required: false, // make the arg required with `required: true`
description: 'output file', // help description
hidden: true, // hide this arg from help
parse: async input => 'output', // instead of the user input, return a different value
default: 'world', // default value if no arg input. Can also be an async function.
defaultHelp: 'a dynamic value' // dynamic default value to show in help output (e.g. current working directory). Can be an async function that returns a string or undefined
options: ['a', 'b'], // only allow input to be from a discrete set
ignoreStdin: false, // set to true to ignore any value provided by stdin
noCacheDefault: false, // if true, the value returned by defaultHelp will not be cached in the oclif.manifest.json.
multiple: false // if true, this argument will support multiple inputs and parse them as an array
}
),
}
Args 导出的参数类型包括: string integer boolean url file directory custom 对于可变长度的参数,使用 multiple: true 属性。注意使用时,该参数之前和之后的所有参数都必须设置为 required: true。 import {Args, Command} from '@oclif/core'
export class MyCLI extends Command {
static args = {
arg1: Args.string({required: true}),
arg2: Args.string({multiple: true, required: true}),
arg3: Args.string({required: true})
}
async run() {
const {args} = await this.parse(MyCLI)
this.log(`arg 1 is ${args.arg1}`) // args.arg1 is a string
this.log(`arg 2 is ${args.arg2.join(',')}`) // args.arg2 is a string[]
this.log(`arg3 is ${args.arg3}`) // args.arg3 is a string
}
}
参数也可通过标准输入(stdin)传入。这在结合 CLI 命令编写脚本时尤为方便。示例如下: $ echo arg1 | mycli
running my command with args: arg1
若需禁用某一参数的此行为,可在其定义中将 ignoreStdin 属性设为 true。