Skip to content

Commit

Permalink
fix(core): fix wrong typing for cmd.option()
Browse files Browse the repository at this point in the history
introduced by 3b2adf9
  • Loading branch information
shigma committed Aug 15, 2021
1 parent 6a8199f commit c79fe94
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 17 deletions.
7 changes: 3 additions & 4 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,9 @@ export class Command<U extends User.Field = never, G extends Channel.Field = nev
return this
}

option<K extends string, T extends keyof Argv.Domain>(name: K, desc: string, config: Argv.OptionConfig<T>): Command<U, G, A, Extend<O, K, Argv.Domain[T]>>
option<K extends string, R extends string>(name: K, desc: string, config: Argv.OptionConfig<R[]>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, R>(name: K, desc: string, config: Argv.OptionConfig<(source: string) => R>): Command<U, G, A, Extend<O, K, R>>
option<K extends string>(name: K, desc: string, config: Argv.OptionConfig<RegExp>): Command<U, G, A, Extend<O, K, string>>
option<K extends string>(name: K, desc: string, config: Argv.TypedOptionConfig<RegExp>): Command<U, G, A, Extend<O, K, string>>
option<K extends string, R>(name: K, desc: string, config: Argv.TypedOptionConfig<(source: string) => R>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, R extends string>(name: K, desc: string, config: Argv.TypedOptionConfig<R[]>): Command<U, G, A, Extend<O, K, R>>
option<K extends string, D extends string>(name: K, desc: D, config?: Argv.OptionConfig): Command<U, G, A, Extend<O, K, Argv.OptionType<D>>>
option(name: string, desc: string, config: Argv.OptionConfig = {}) {
this._createOption(name, desc, config)
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function formatCommands(path: string, session: Session<ValidationField>, childre
return output
}

function getOptionVisibility(option: Argv.OptionConfig, session: Session<ValidationField>) {
function getOptionVisibility(option: Argv.OptionDeclaration, session: Session<ValidationField>) {
if (session.user && option.authority > session.user.authority) return false
return !session.resolveValue(option.hidden)
}
Expand Down
6 changes: 5 additions & 1 deletion packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ export namespace Argv {
notUsage?: boolean
}

export interface TypedOptionConfig<T extends Type> extends OptionConfig<T> {
type: T
}

export interface OptionDeclaration extends Declaration, OptionConfig {
description?: string
values?: Record<string, any>
Expand All @@ -377,7 +381,7 @@ export namespace Argv {
this.declaration = decl.stripped
}

_createOption(name: string, def: string, config?: OptionConfig) {
_createOption(name: string, def: string, config: OptionConfig) {
const param = paramCase(name)
const decl = def.replace(/(?<=^|\s)[\w\x80-\uffff].*/, '')
const desc = def.slice(decl.length)
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-core/tests/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ describe('Parser API', () => {
cmd.option('beta', '-b <beta>')
// infer argument type from fallback
cmd.option('gamma', '-c <gamma>', { fallback: 0 })
// define argument type by OptionConfig
cmd.option('delta', '-d <delta>', { type: 'string' })
// define argument type by definition
cmd.option('delta', '-d <delta:string>')
// define argument type directly (should not be overrode by default)
cmd.option('epsilon', '-e <epsilon:posint>', { fallback: 1 })
})
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-monitor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function apply(ctx: Context) {
cmd.subcommand('.create <...names>', '添加新的监测账号', { authority: 3 })
.option('bilibili', '-b <id> 设置 Bilibili 账号')
.option('mirrativ', '-m <id> 设置 Mirrativ 账号')
.option('twitcasting', '-t <id> 设置 TwitCasting 账号', { type: 'string' })
.option('twitcasting', '-t <id:string> 设置 TwitCasting 账号')
.action(async ({ options }, ...names) => {
if (!names.length) return '请提供至少一个名字。'

Expand Down Expand Up @@ -96,11 +96,11 @@ export function apply(ctx: Context) {
})

cmd.subcommand('.update <name>', '修改已有账号信息', { authority: 3 })
.option('addName', '-n <name> 添加账号名', { type: 'string', fallback: '' })
.option('removeName', '-N <name> 删除账号名', { type: 'string', fallback: '' })
.option('bilibili', '-b <id> 设置 Bilibili 账号', { type: 'string' })
.option('mirrativ', '-m <id> 设置 Mirrativ 账号', { type: 'string' })
.option('twitcasting', '-t <id> 设置 TwitCasting 账号', { type: 'string' })
.option('addName', '-n <name:string> 添加账号名', { fallback: '' })
.option('removeName', '-N <name:string> 删除账号名', { fallback: '' })
.option('bilibili', '-b <id:string> 设置 Bilibili 账号')
.option('mirrativ', '-m <id:string> 设置 Mirrativ 账号')
.option('twitcasting', '-t <id:string> 设置 TwitCasting 账号')
.action(async ({ options }, name: string) => {
if (!name) return '请输入账号。'
name = String(name)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export function apply(ctx: Context, config: Config = {}) {
ctx1.command('shot <url> [selector:rawtext]', '网页截图', { authority: 2 })
.alias('screenshot')
.option('full', '-f 对整个可滚动区域截图')
.option('viewport', '-v <viewport> 指定视口', { type: 'string' })
.option('viewport', '-v <viewport:string> 指定视口')
.action(async ({ session, options }, url, selector) => {
if (!url) return '请输入网址。'
const scheme = /^(\w+):\/\//.exec(url)
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-tools/src/music.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function apply(ctx: Context, options: MusicOptions = {}) {

ctx.command('tools/music <name:text>', '点歌')
// typescript cannot infer type from string templates
.option('platform', `-p <platform> 点歌平台,目前支持 qq, netease,默认为 ${platform}`, { type: 'string' })
.option('platform', `-p <platform> 点歌平台,目前支持 qq, netease,默认为 ${platform}`, { type: Object.keys(platforms) })
.alias('点歌')
.shortcut('来一首', { fuzzy: true })
.shortcut('点一首', { fuzzy: true })
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-tools/src/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const name = 'weather'
export function apply(ctx: Context) {
ctx.command('tools/weather <longitude> <latitude>', '查询天气')
// typescript cannot infer type from string templates
.option('product', `-p <product> 晴天钟产品选择,可为 ${products.join(', ')}`, { type: 'string', fallback: 'civil' })
.option('product', `-p <product:string> 晴天钟产品选择,可为 ${products.join(', ')}`, { fallback: 'civil' })
.action(async ({ options }, lon, lat) => {
if (!lon || !lat) return '请输入经纬度。'
const { product } = options
Expand Down

0 comments on commit c79fe94

Please sign in to comment.