【问题标题】:Command Line Radio Buttons/Options in RubyRuby 中的命令行单选按钮/选项
【发布时间】:2018-03-03 18:00:18
【问题描述】:
我们如何制作这样的命令行选项:
我会想象代码看起来像这样
options = Hash.new()
options['Monolithic'] = 'Monolithic application'
options['Microservice'] = 'Microservice application'
options['Gateway'] = 'Microservice gateway'
puts 'Which *type* of application would you like to create?'
options.each do |key, option|
puts option
end
# interface here
【问题讨论】:
标签:
ruby
command-line
command-line-interface
【解决方案2】:
我写了tty-prompt gem 来帮助构建交互式菜单。该示例的实现如下所示:
require "tty-prompt"
prompt = TTY::Prompt.new
type = prompt.decorate("*type*", :yellow)
prompt.select("Which #{type} of application would you like to create?") do |menu|
menu.choice "Monolithic application", "Monolithic"
menu.choice "Microservice application", "Microservice"
menu.choice "Microservice gateway", "Gateway"
end
上面将在控制台中呈现以下选择菜单:
此 gem 经测试可在多种操作系统上运行,并且有多种可用的提示类型。