簡體   English   中英

如何在thor中使用或添加子命令功能?

[英]How to use or add subcommand feature with thor?

我正在使用thor創建一個CLI應用程序。 它運行的很好,但是現在我仍然使用子命令功能。

它的github Wiki上沒有任何內容,並且在Google周圍搜索,但沒有任何幫助。

那么,有人可以向我展示或指出如何實現子命令功能嗎?

檢出: 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

心連心...

嘗試這樣的事情(文件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 bar”的幫助信息不太正確之外,這基本上可以按預期工作。恕我直言。我認為應該說“ test.rb bar baz ...”,而不是“ test.rb baz”。 ..“。)

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM