2011-06-17 61 views

回答

1

嘗試是這樣的(文件test.rb):

#!/usr/bin/env ruby 

require 'rubygems' 
require 'thor' 
require 'thor/group' # This is required -- it's not a bug, it's a feature! 

class Bar < Thor 
    desc "baz", "Whatever" 
    def baz 
    puts "Hello from Bar" 
    end 
end 

class Foo < Thor 
    desc "go", "Do something" 
    def go 
    puts "Hello there!" 
    end 

    register Bar, :bar, "bar", "Do something else" 
end 

if __FILE__ == $0 
    Foo.start 
end 

此行爲如下:

> test.rb 
Tasks: 
    test.rb bar   # Do something else 
    test.rb go   # Do something 
    test.rb help [TASK] # Describe available tasks or one specific task 

> test.rb go 
Hello there! 
> test.rb bar 
Tasks: 
    test.rb baz    # Whatever 
    test.rb help [COMMAND] # Describe subcommands or one specific subcommand 

> test.rb bar baz 
Hello from Bar 
> test.rb baz 
Could not find task "baz". 
> 

(這主要按預期工作,除了「test.rb的幫助信息酒吧「是不完全正確的,恕我直言,我認爲它應該說」test.rb酒吧巴茲...「,而不是」test.rb baz ...「。)

希望這有助於!

+0

這是有幫助的。但我會以另一種方式實施它。有一個子命令方法。 – Autodidact 2011-06-22 04:04:24

3

退房:http://whatisthor.com/

從上述站點(編輯了一下,以節省空間,並突出使用子):

 
module GitCLI 
    class Remote ", "Adds a remote named for the repository at " 
    option :t, :banner => "" 
    option :m, :banner => "" 
    options :f => :boolean, :tags => :boolean, :mirror => :string 
    def add(name, url) 
     # implement git remote add 
    end 

    desc "rename ", "Rename the remote named to " 
    def rename(old, new) 
    end 
    end 

    class Git [...]", "Download objects and refs from another repository" 
    options :all => :boolean, :multiple => :boolean 
    option :append, :type => :boolean, :aliases => :a 
    def fetch(respository, *refspec) 
     # implement git fetch here 
    end 

    desc "remote SUBCOMMAND ...ARGS", "manage set of tracked repositories" 
    subcommand "remote", Remote ### SUBCOMMAND USED HERE... 
    end 
end 

心連心......

相關問題