Thor 中的 Limited_options 的别名

问题描述 投票:0回答:1

我想要求用户在 Thor 中输入内容,但有别名:

$ Is this correct?: [yes, no, maybe] (yes)

目前我有这个:

class CLI < Thor
  desc 'do_thing', 'does a thing'
  def do_thing      
    answer = ask("Is this correct?", limited_to: %w{yes no maybe}, default: 'yes')
    # do stuff
  end
end

我希望用户只输入每个选项的第一个字母。

但我得到了这样的回应:

$ Your response must be one of: [yes, no, maybe]. Please try again.
ruby command-line-interface thor
1个回答
0
投票

您无法向

ask
添加别名。

如果你真的想要这个功能,你最好的可能是这样的:

class CLI < Thor
  desc 'do_thing', 'does a thing'
  def do_thing      
    answer = ask("Is this correct? (Y)es/(N)o/(M)aybe", limited_to: %w{y n m}, default: 'y', case_insensitive: true)
    # do stuff
  end
end

输出为:

Is this correct? (Y)es/(N)o/(M)aybe [y,n,m] (y)
© www.soinside.com 2019 - 2024. All rights reserved.