LoginSignup
65

More than 5 years have passed since last update.

posted at

updated at

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

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

リンク

続き

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
What you can do with signing up
65