【问题标题】:Ruby highline gem: Is there a way to assign a command for the duration of the script/appRuby highline gem:有没有办法在脚本/应用程序的持续时间内分配命令
【发布时间】:2013-11-03 16:48:24
【问题描述】:

我有使用 highline 的 gem/cli,我想知道您是否可以设置自己的命令以便它始终可用(类似于“帮助”)。

require 'rubygems'
require 'highline/import'

say("\nThis is the new mode (default)...")
choose do |menu|
  menu.prompt = "Please choose your favorite programming language?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

say("\nThis is letter indexing...")
choose do |menu|
  menu.index        = :letter
  menu.index_suffix = ") "

  menu.prompt = "Please choose your favorite programming language?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

say("\nThis is with a different layout...")
choose do |menu|
  menu.layout = :one_line

  menu.header = "Languages"
  menu.prompt = "Favorite?  "

  menu.choice :ruby do say("Good choice!") end
  menu.choices(:python, :perl) do say("Not from around here, are you?") end
end

谢谢!

【问题讨论】:

  • @Felix 希望上面的代码更清楚 :) 如果运行上面的代码,您可以随时键入“帮助”来获取帮助菜单。我想创建自己的不同命令,例如“退出”或“菜单”,以便随时退出或调出不同的菜单。
  • doh,抱歉,我一定是忘记点击保存了,它现在就在那里

标签: ruby gem command-line-interface highline


【解决方案1】:

我认为这只有通过对 highline gem 进行相当诡异的猴子修补才能实现,除非您想将命令添加到每个选项中(您最喜欢的编程语言是什么?1. ruby​​ 2. perl 3. help 4. menu 5. 退出...),您可以通过以下方法提取它:

def add_custom_choices(menu)
  menu.choice(:quit) do
    say "Ok, see you."
    exit 0
  end
  menu.choice(:dostuff) do call_do_stuff_method end
end


# and later ...
choose do |menu|
  # ...
  menu.choice :ruby do say("Good choice!") end
  add_custom_choices menu
  # ....
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多