Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: 负数参数被错误的识别成选项 #1147

Closed
Tracked by #1139
ENDsoft233 opened this issue Jul 17, 2023 · 3 comments
Closed
Tracked by #1139

Bug: 负数参数被错误的识别成选项 #1147

ENDsoft233 opened this issue Jul 17, 2023 · 3 comments

Comments

@ENDsoft233
Copy link

ENDsoft233 commented Jul 17, 2023

Describe the bug

如题,负数参数会被错误的识别成选项。

一般说来,所有选项需要被放置在所有的参数之前,在开始读入参数之后,不应该再定义短横杠 - 为选项标志。

此外,在没有定义选项的参数之中,短横杠 - 也不应该被定义为选项标志。

Steps to reproduce

  1. 定义命令如下:
ctx
  .command('echo <digit:number>', { showWarning: true, checkArgCount: true })
  .action(({ }, digit) => <>{digit}</>)
  1. 执行命令
echo 1 // returns 1
echo -1 // throw error
echo "-1" // returns -1
  1. 定义命令如下:
ctx
  .command('echo <string> <digit:number>', { showWarning: true, checkArgCount: true })
  .action(({ }, string, digit) => <>{digit}</>)
  1. 执行命令
echo x 1 // returns 1
echo x -1 // throw error
echo x "-1" // returns -1

Expected behavior

满足一定的前提下,负数可以被正确识别,简化输入逻辑。

这里的前提包括但不限于

  • 指令没有定义选项
  • 选项读取完毕,已经开始读入参数
  • 读取时已知此位置期望值是一个数值时(尝试转化为数值读入)

Screenshots

029ce16e54d767037a7318aef8597a22.png

1199cf6712df6714fa51d6ca34dc376b.png

image.png

image.png

Versions

  • OS: Windows 11 (10.0.22621.1702)
  • Platform: onebot
  • Node version: 18.13.0
  • Koishi version: 4.13.2

Additional context

No response

@ENDsoft233 ENDsoft233 added the bug BUG label Jul 17, 2023
@ilharp
Copy link
Member

ilharp commented Jul 17, 2023

一般说来,所有选项需要被放置在所有的参数之前,在开始读入参数之后,不应该再定义短横杠 - 为选项标志。

我在搜索网络资源后并没有找到相关的说法。如果可以的话希望能够提供一些资料,或是其他应用中的示例。我个人经常在指令末尾附加选项,因此我觉得这个观点并没有成为共识。此外,很多著名的 Koishi 插件 提供的示例也在指令末尾附加选项。


个人推荐的解决方法是 Double Dash。这是 Bash 标准,也被现今的大部分 CLI 应用所采用。依稀记得 Koishi 在很久以前实现了此标准,但我刚才测试似乎并不生效。

@ENDsoft233
Copy link
Author

一般说来,所有选项需要被放置在所有的参数之前,在开始读入参数之后,不应该再定义短横杠 - 为选项标志。

我在搜索网络资源后并没有找到相关的说法。如果可以的话希望能够提供一些资料,或是其他应用中的示例。我个人经常在指令末尾附加选项,因此我觉得这个观点并没有成为共识。此外,很多著名的 Koishi 插件 提供的示例也在指令末尾附加选项。

个人推荐的解决方法是 Double Dash。这是 Bash 标准,也被现今的大部分 CLI 应用所采用。依稀记得 Koishi 在很久以前实现了此标准,但我刚才测试似乎并不生效。

抱歉,是我的认知错误,之前可能是用错了,导致我一直以为选项只能写在开头(

我测试了下,选项是可以写在指令末尾的

ctx
  .command('echo [...digit:number]', { showWarning: true, checkArgCount: true })
  .option('trans', '-t')
  .action(({ options }, ...digit) => <>{options.trans ? digit.map(d => -d).join(', ') : digit.join(', ')}</>)
echo 1 2 3 -t // returns -1, -2, -3

@shigma
Copy link
Member

shigma commented Jul 17, 2023

这个问题从本质上无法解决,因为选项优先于数字解析,用户需要有意识地使用引号进行转义。

但我非常认同本 issue 的场景是普遍发生的,因而值得被专门优化。我能想到的方案有:

  1. 如果一个选项需要带 number 类型参数,那么其后紧跟出现的负数被识别为其参数而不是选项
  2. 如果一个指令带 number 类型参数,那么在对应位置出现的负数都被识别为其参数而不是选项
  3. 任意位置出现的负数,只要该指令没有声明对应的选项或选项别名,均视为负数

我个人倾向于只做 1, 2。方案三有点破坏一致性,而且 1, 2 本身就应当覆盖大部分情况。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants