How to write a Ruby command line app that supports tab completion?

后端 未结 3 1111
野趣味
野趣味 2020-12-23 10:25

I want to write a command line app, a shell if you will, in Ruby.

I want the user to be able to press Tab at certain points and offer completion of values.

H

3条回答
  •  旧巷少年郎
    2020-12-23 11:20

    Ah, it seems the standard library is my friend after all. What I was looking for is the Readline library.

    Doc and examples here: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/readline/rdoc/Readline.html

    In particular, this is a good example from that page to show how completion works:

    require 'readline'
    
    LIST = [
      'search', 'download', 'open',
      'help', 'history', 'quit',
      'url', 'next', 'clear',
      'prev', 'past'
    ].sort
    
    comp = proc { |s| LIST.grep(/^#{Regexp.escape(s)}/) }
    
    Readline.completion_append_character = " "
    Readline.completion_proc = comp
    
    while line = Readline.readline('> ', true)
      p line
    end
    

    NOTE: The proc receives only the last word entered. If you want the whole line typed so far (because you want to do context-specific completion), add the following line to the above code:

    Readline.completer_word_break_characters = "" #Pass whole line to proc each time
    

    (This is by default set to a list of characters that represent word boundaries and causes only the last word to be passed into your proc).

提交回复
热议问题