lib/travis/cli/command.rb in travis-1.6.15.travis.566.6 vs lib/travis/cli/command.rb in travis-1.6.15.travis.580.6

- old
+ new

@@ -66,10 +66,25 @@ def self.description(description = nil) @description = description if description @description ||= "" end + def self.subcommands(*list) + return @subcommands ||= [] if list.empty? + @subcommands = list + + define_method :run do |subcommand, *args| + error "Unknown subcommand. Available: #{list.join(', ')}." unless list.include? subcommand.to_sym + send(subcommand, *args) + end + + define_method :usage do + usages = list.map { |c| color(usage_for("#{command_name} #{c}", c), :command) } + "\nUsage: #{usages.join("\n ")}\n\n" + end + end + attr_accessor :arguments, :config, :force_interactive, :formatter, :debug attr_reader :input, :output alias_method :debug?, :debug def initialize(options = {}) @@ -195,22 +210,25 @@ def command_name self.class.command_name end def usage - usage = "travis #{command_name}" - method = method(:run) + "Usage: " << color(usage_for(command_name, :run), :command) + end + + def usage_for(prefix, method) + usage = "travis #{prefix}" + method = method(method) if method.respond_to? :parameters method.parameters.each do |type, name| name = "[#{name}]" if type == :opt name = "[#{name}..]" if type == :rest usage << " #{name}" end elsif method.arity != 0 usage << " ..." end usage << " [options]" - "Usage: " << color(usage, :command) end def help(info = "") parser.banner = usage self.class.description.sub(/./) { |c| c.upcase } + ".\n" + info + parser.to_s