Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating interactive ruby console application

I want make interactive application where user launches it and can do various task by typing commands (some kind of shell)

example:

./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)

I searched but couldn't find any ruby gems that I could use specially for console interaction, so I'm about to my make my own...

I looked at Thor, but that's not exactly as I want, maybe I could use it, but not sure...

it could look something like:

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])

so my question is, what gems I could use to make such console class? maybe someone have already made something similar? I plan using HighLine for input/output, but any other suggestion what could I use?

like image 526
davispuh Avatar asked Mar 24 '12 17:03

davispuh


People also ask

How do I start ruby interactive console?

Open Interactive Ruby Open up IRB (which stands for Interactive Ruby). If you're using macOS open up Terminal and type irb, then hit enter. If you're using Linux, open up a shell and type irb and hit enter. If you're using Windows, open Interactive Ruby from the Ruby section of your Start Menu.

Is there a ruby shell?

Interactive Ruby Shell (IRB or irb) is a REPL for programming in the object-oriented scripting language Ruby. The abbreviation irb is a portmanteau of the word "interactive" and the filename extension for Ruby files, ".

How do I start rails console?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .

What is ruby command?

Ruby command is a free and open source programming language; it is flexible and is feature rich. As the name suggests, ruby indeed is a jewel language which comes at a very low entry cost. Its plug and play capability and also easily readable syntax makes it very user-friendly.


1 Answers

What you want is a REPL – Read → Evaluate → Print Loop.

IRB, for example, implements a REPL for the Ruby language.

Here's a very simple implementation of your application's 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 and \z match the start of the string and the end of the string, respectively.

like image 88
Matheus Moreira Avatar answered Sep 22 '22 12:09

Matheus Moreira