Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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

リンク

続き

ongaeshi
ソースコード検索アプリのMilkodeとかを作っています。 ブログ http://ongaeshi.hatenablog.com/
http://ongaeshi.me/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした