Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
66
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

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

リンク

続き

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
66
Help us understand the problem. What are the problem?