Thor - Building CLI with ruby

A33de8445891945c81e488c9ce71e8cc?s=47 Mann
September 08, 2012

Thor - Building CLI with ruby

Create command-line interface with thor

A33de8445891945c81e488c9ce71e8cc?s=128

Mann

September 08, 2012
Tweet

Transcript

  1. Building command-line interface (CLI) with ruby

  2. I have learnt ruby the rails way. http://www.frankwatching.com/wordpress/wp-content/uploads/2007/11/rails.jpg

  3. Then... tryruby.org rubykoans.com rubeque.com more and more ruby

  4. A rails app is a ruby app. A ruby app

    may NOT be a rails app.
  5. Today Ruby on CLI

  6. What for? Utility => curl Code Genertor => Rails generator

    Automation => db migration
  7. Hello world puts "Hello world"

  8. My ideal CLI mycli <command> [options]

  9. I need to parse the command and options in mycli

    manually. ARGV or
  10. None
  11. Multiple commands desc "hi", "Say hi" def hi puts "hi

    !" end desc "bye", "Say goodbye" def bye puts "bye !" end
  12. Passing argument desc "hello NAME", "Say hello to <NAME>" def

    hello(name) puts "hello #{name}!" end
  13. Parsing options desc "hello NAME", "Say hello to <NAME>" method_options

    loudly: false def hello(name) name.upcase! if options[:loudly] puts "hello #{name}!" end
  14. Documentation ready

  15. Thanks @lchanmann

  16. References http://whatisthor.com/ http://yehudakatz.com/2009/01/13/some-thor-news/ http://rubylearning.com/blog/2011/01/03/how-do-i-make-a-command-line-tool-in-ruby/ http://www.bounga.org/ruby/2011/09/12/thor-a-command-line-scripting-tool/ Demo: https://gist.github.com/3688473 Checkout thor on

    Github https://github.com/wycats/thor http://whatisthor.com/images/thor.png