2012-03-24 78 views
12

我想使交互式應用,其中用戶運行它,並可以通過創建交互式Ruby控制檯應用程序

例如鍵入命令(某種殼)做各種任務:

./myapp.rb 
App says Hi 
Commands: 
    help - display help about command 
    open - open task 
    do - do action 
Start>help open 
    open <TaskName> 
    opens specified task 
Start>open Something 
Something>do SomeAction 
    Success! 
Something> (blinking cursor here) 

我搜查,但couldn沒有找到任何寶石寶石,我可以專門用於控制檯交互,所以我即將做出我自己的...

我看着Thor,但那不完全如我wa NT,也許我可以使用它,但不知道...

它可能看起來像:

class Tasks 
    attr_reader :opened_task 

    desc "open <TaskName>", "opens specified task" 
    def open(params) 
    end 

    desc "do <ActionName>", "do specified action" 
    def do(params) 
    end 
end 

tasks = Tasks.new 
# theoretical Console class 
console = Console.new 
console.addCommand("open",tasks.method(:open),"open task") 
console.addCommand("do",tasks.method(:do),"do action") 
console.start("%s>",[*tasks.opened_task]) 

所以我的問題是,什麼寶石我可以用它來做出這樣的控制檯類?也許有人已經做出了類似的東西? 我計劃使用HighLine進行輸入/輸出,但任何其他建議我可以使用什麼?

+0

它應該是紅寶石或你自己的語法? – Reactormonk 2012-03-24 19:02:49

+0

你是指紅寶石還是自己的語法? :| 如果你的意思是使用IRB,那麼這不是一個選項... – davispuh 2012-03-24 19:11:52

+1

爲什麼不呢?它爲您免費提供圖靈完整性。 – Reactormonk 2012-03-24 19:15:04

回答

17

你想要的是一個REPL - Read → Evaluate → Print Loop。例如,IRB爲Ruby語言實現了REPL。

這裏是一個非常簡單的實現應用程序的REPL的:

loop do 
    Application::Console.prompt.display 
    input = gets.chomp 
    command, *params = input.split /\s/ 

    case command 
    when /\Ahelp\z/i 
    puts Application::Console.help_text 
    when /\Aopen\z/i 
    Application::Task.open params.first 
    when /\Ado\z/i 
    Application::Action.perform *params 
    else puts 'Invalid command' 
    end 
end 

\A\z匹配的字符串分別開始和字符串的結尾。

+0

好吧謝謝顯示的方式之一,我可以如何實現控制檯類的方法「開始」:) – davispuh 2012-03-24 20:08:58

+0

@davispuh我忘了提示 - 更新答案改進實施。你的'start'方法應該只包含循環。 – 2012-03-24 20:28:55

+2

我接受了這個答案,因爲它建議通過展示部分實現來自己製作它,所以我將製作自​​己的CLI,特別是爲了我所有的需要... – davispuh 2012-03-26 22:19:20

1
class MyAPI 
    def self.__is__(text) 
    @__is__ = text 
    end 

    def self.method_added(method) 
    @__help__ ||= {} 
    @__help__[method.to_s] = @__is__ 
    @__is__ = nil 
    end 

    def self.help(of) 
    @__help__[of] 
    end 

    __is__ "open file <file>" 
    def open(file) 
    #... 
    end 

    __is__ "do X" 
    def do(*params) 
    #... 
    end 

    __is__ "calls help, use help <command>" 
    def help(*args, &block) 
    self.class.help(*args, &block) 
    end 
end 

MyAPI.new(...).pry 

或者你可以使用撬命令,但擊敗 圖靈完備。可能使用命令來實現幫助,因爲我不知道我的方法效果如何。這些方法需要編碼爲 防禦性。我不記得如何使用類變量: -/

+0

它需要大量的更改/配置才能讓它工作,因爲我需要...我也會用它來完成任務,而這個任務並不適用於它,它具有「功能」,我甚至不需要... – davispuh 2012-03-26 21:10:42

+0

@davispuh,你給了'cliqr'(上面)一個鏡頭嗎?它不需要太多的配置,幾乎可以直接使用。 – nuaavee 2018-02-09 02:25:41

+0

@nuaavee當我寫下這個問題時,它現在還沒有存在,但它看起來與我所需要的非常相似,然後比創建它早了3年:D – davispuh 2018-02-09 18:37:42

4

您也可以嘗試ripl。 (從文件):

require 'ripl' 
# Define plugins, load files, etc... 
Ripl.start 

有插件RIPL的完整列表以及使用項目網站上RIPL控制檯應用程序列表: 創建和啓動自定義外殼的就是這麼簡單。

+0

這看起來更有希望,但仍需要做一些配置,並且它有相同的「功能」,我不需要作爲pry/irb,我也認爲需要非常多的變化/配置... – davispuh 2012-03-26 22:15:45

+2

說實話,迄今爲止我沒有使用'ripl' - 但是寫入從頭開始你自己的解決方案似乎不是最好的主意。只要考慮用箭頭鍵移動 - 沒有readline支持用戶會發誓。 – 2012-03-26 22:24:35

+0

在Windows中,命令歷史正在工作(向上/向下箭頭),我只需要使用選項卡自動完成,但在開始時我甚至可能沒有這樣的生活...... – davispuh 2012-03-27 01:21:05

4

好的,所以我創建了這個庫,用於在ruby中創建控制檯應用程序。實際上它是前一陣子,但只是決定釋放它。如果與HighLine和Readline一起使用,它支持自動完成。

當我寫它沒有任何文檔和測試/規格,但現在我做了一些。仍然不多,但開始應該沒問題。

所以寶石cli-console和 代碼是在GitHub上,這裏的usage example

3

看看cliqr紅寶石的寶石。它看起來正是你所需要的。以下是描述性自述文件的github鏈接:https://github.com/anshulverma/cliqr

它可以直接或在內置shell中執行命令。

下面是它的混帳回購測試用例:

it 'can execute a sub action from shell' do 
     cli = Cliqr.interface do 
     name 'my-command' 
     handler do 
      puts 'base command executed' 
     end 

     action :foo do 
      handler do 
      puts 'foo executed' 
      end 

      action :bar do 
      handler do 
       puts 'bar executed' 
      end 
      end 
     end 
     end 

     with_input(['', 'my-command', 'foo', 'foo bar', 'foo bar help']) do 
     result = cli.execute %w(my-command shell), output: :buffer 
     expect(result[:stdout]).to eq <<-EOS 
Starting shell for command "my-command" 
my-command > . 
base command executed 
my-command > my-command. 
base command executed 
my-command > foo. 
foo executed 
my-command > foo bar. 
bar executed 
my-command > foo bar help. 
my-command foo bar 

USAGE: 
    my-command foo bar [actions] [options] [arguments] 

Available options: 

    --help, -h : Get helpful information for action "my-command foo bar" along with its usage information. 

Available actions: 
[ Type "my-command foo bar help [action-name]" to get more information about that action ] 

    help -- The help action for command "my-command foo bar" which provides details and usage information on how to use the command. 
my-command > exit. 
shell exited with code 0 
     EOS 
     end 
    end 
3

TTY是很容易做這樣的事情一個很好的寶石。你有足夠的工具可以單獨使用或者使用完整的toolkit。您可以使用顏色,提示,執行shell本機,與屏幕交互,打印表格,進度條和其他許多有用的命令行元素,以及簡單的goop api。

特別是tty-prompt對詢問用戶輸入非常有用。

你提出的情況下,一個簡單的例子:

require 'tty-prompt' 
require 'pastel' 

prompt = TTY::Prompt.new 
loop do 
    cmd, parms* = prompt.ask('[email protected]$ ').split /\s/ 
    case cmd 
    when "hola" 
     puts "Hola amigo " parms 
    when "exit" 
     break if prompt.yes?('Do you really want to exit?') 
    end 
end 
+0

感謝您的評論。我正要增加這個例子。 – 2017-09-25 15:21:18

+0

這不是我正在尋找的東西,這是一個可以用來實現這種控制檯的庫,如你所示。當我問這個問題的時候,它也不存在:D我使用https://github.com/JEG2/highline實現了我的CLI控制檯,它的功能非常類似。 – davispuh 2017-10-01 17:56:10

相關問題