koishijs / koishi

Cross-platform chatbot framework made with love
https://koishi.chat
MIT License
4.4k stars 241 forks source link

Bug: 当给 option 设置了 fallback 后,alias 中的 config.options 无效 #1437

Closed SaarChaffee closed 1 month ago

SaarChaffee commented 2 months ago

Describe the bug

当给 option 设置了 fallback 后,alias 中的 config.options 无效

Steps to reproduce

ctx
  .command('cmd <cmd:string>')
  .option('region', '-r <region:string>', { fallback: 'cn' })
  .alias('tcmd', { options: { region: 'tw' } })
  .alias('rcmd', { options: { region: 'jp' } })
  .action(async ({ session, options }, cmd) => {
    ctx.logger.info(cmd)
    ctx.logger.info(options)
  })
message: [ '#cmd 123' ]
cmd: 123
option: { region: 'cn' }

message: [ '#tcmd123' ]
cmd: 123
option: { region: 'cn' }

message: [ '#rcmd 123' ]
cmd: 123
option: { region: 'cn' }

Expected behavior

region 应该与 alias 中传入的一致

Screenshots

No response

Versions

System: OS: Windows 10 10.0.19045 CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz

Binaries: Node: 22.5.1 Yarn: 4.1.1

Koishi: Core: 4.17.10 Console: 5.29.3

Additional context

No response

Hieuzest commented 1 month ago

Currently the pre-defined args of aliases is broken by the fix.

https://github.com/koishijs/koishi/blob/2e7e3e5657106723e6ce6c5299f095af2ac6d914/packages/core/src/command/index.ts#L316-L317 should be pure re-assignment to avoid redundant, and

https://github.com/koishijs/koishi/blob/2e7e3e5657106723e6ce6c5299f095af2ac6d914/packages/core/src/command/parser.ts#L376 should be [...argv.args] otherwise it will modify alias definition