LoginSignup
64
65

More than 5 years have passed since last update.

Rubyでサブコマンド付きのコマンドを簡単に書けるThor

Last updated at Posted at 2012-05-12

gitやsvnのようなサブコマンド付きのコマンドが・・

$ demo 
Tasks:
  demo abc           # print 'abc'
  demo efg [option]  # print 'efg'
  demo help [TASK]   # Describe available tasks or one specific task

$ demo abc
abc!

$ demo zzz
Could not find task "zzz".

$ demo efg
efg!

$ demo efg -u
EFG!

$ demo help efg
Usage:
  demo efg [option]

Options:
  -u, [--upcase]  # disp upcase

print 'efg'

これ位の記述量で書けるようになります。

demo
#! /usr/bin/env ruby

require 'rubygems'
require 'thor'

class Demo < Thor
  desc "abc", "print 'abc'"
  def abc
    puts 'abc!'
  end

  desc "efg [option]", "print 'efg'"
  option :upcase, :type => :boolean, :aliases => '-u', :desc => "disp upcase"
  def efg
    if options[:upcase]
      puts 'EFG!'
    else
      puts 'efg!'
    end
  end
end

Demo.start

Milkode 0.7milkコマンドの内部で利用しました。

インストール

$ gem install thor

リンク

続き

64
65
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
64
65