我正在使用thor創建CLI應用程序。它很好,但現在我堅持使用子命令功能。如何在thor中使用或添加子命令功能?
它的github wiki上沒有任何內容,並且沒有任何幫助。
那麼,有人可以顯示或指出我如何實現子命令功能?
我正在使用thor創建CLI應用程序。它很好,但現在我堅持使用子命令功能。如何在thor中使用或添加子命令功能?
它的github wiki上沒有任何內容,並且沒有任何幫助。
那麼,有人可以顯示或指出我如何實現子命令功能?
嘗試是這樣的(文件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 ...「。)
希望這有助於!
從上述站點(編輯了一下,以節省空間,並突出使用子):
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
心連心......
這是有幫助的。但我會以另一種方式實施它。有一個子命令方法。 – Autodidact 2011-06-22 04:04:24