进阶 oclif.io 2026-10-18 02:37:03 · 29 阅读
第15章 OCLif 指南:自定义基类与共享 Flag 实践
# 自定义基类
通过继承来在多个命令之间共享功能。下面是一个命令基类的示例,其中包含一些通用的共享 flag。
对于拥有多个插件的大型 CLI 来说,建议把这个基类放进一个独立的 npm 包中,以便各处共享。
// src/baseCommand.ts
import {Command, Flags, Interfaces} from '@oclif/core'
export type Flags = Interfaces.InferredFlags
export type Args = Interfaces.InferredArgs
export abstract class BaseCommand extends Command {
// 添加 --json flag
static enableJsonFlag = true
// 定义可被任何继承 BaseCommand 的命令复用的 flags
static baseFlags = {
'log-level': Flags.option({
default: 'info',
helpGroup: 'GLOBAL',
options: ['debug', 'warn', 'error', 'info', 'trace'] as const,
summary: '指定日志级别。',
})(),
}
protected flags!: Flags
protected args!: Args
public async init(): Promise {
await super.init()
const {args, flags} = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
enableJsonFlag: this.ctor.enableJsonFlag,
args: this.ctor.args,
strict: this.ctor.strict,
})
this.flags = flags as Flags
this.args = args as Args
}
protected async catch(err: Error & {exitCode?: number}): Promise {
// 在这里添加处理命令错误的自定义逻辑,
// 或者直接沿用父类的错误处理
return super.catch(err)
}
protected async finally(_: Error | undefined): Promise {
// 无论命令是否出错,run 和 catch 之后都会调用此方法
return super.finally(_)
}
}
// src/commands/my-command.ts
export default class MyCommand extends BaseCommand {
static summary = '继承 BaseCommand 的子类'
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --json',
'<%= config.bin %> <%= command.id %> --log-level debug',
]
static flags = {
name: Flags.string({
char: 'n',
summary: '要打印的名称。',
required: true,
}),
}
public async run(): Promise> {
for (const [flag, value] of Object.entries(this.flags)) {
this.log(`${flag}: ${value}`)
}
return this.flags
}
}
以 Salesforce CLI 为例,展示了如何处理更复杂的场景。
import {Command, Flags, Interfaces} from '@oclif/core'
export type Flags
export type Args
export abstract class BaseCommand
// 添加 --json flag
static enableJsonFlag = true
// 定义可被任何继承 BaseCommand 的命令复用的 flags
static baseFlags = {
'log-level': Flags.option({
default: 'info',
helpGroup: 'GLOBAL',
options: ['debug', 'warn', 'error', 'info', 'trace'] as const,
summary: '指定日志级别。',
})(),
}
protected flags!: Flags
protected args!: Args
public async init(): Promise
await super.init()
const {args, flags} = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
enableJsonFlag: this.ctor.enableJsonFlag,
args: this.ctor.args,
strict: this.ctor.strict,
})
this.flags = flags as Flags
this.args = args as Args
}
protected async catch(err: Error & {exitCode?: number}): Promise
// 在这里添加处理命令错误的自定义逻辑,
// 或者直接沿用父类的错误处理
return super.catch(err)
}
protected async finally(_: Error | undefined): Promise
// 无论命令是否出错,run 和 catch 之后都会调用此方法
return super.finally(_)
}
}
// src/commands/my-command.ts
export default class MyCommand extends BaseCommand
static summary = '继承 BaseCommand 的子类'
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --json',
'<%= config.bin %> <%= command.id %> --log-level debug',
]
static flags = {
name: Flags.string({
char: 'n',
summary: '要打印的名称。',
required: true,
}),
}
public async run(): Promise
for (const [flag, value] of Object.entries(this.flags)) {
this.log(`${flag}: ${value}`)
}
return this.flags
}
}
以 Salesforce CLI 为例,展示了如何处理更复杂的场景。