Class: Clio::Usage::Command

Inherits:
Subcommand show all
Defined in:
lib/clio/usage/command.rb

Overview

Command

This is the toplevel “main” command.

Instance Attribute Summary

Attributes inherited from Subcommand

#arguments, #help, #name, #options, #parent, #subcommands

Instance Method Summary collapse

Methods inherited from Subcommand

#===, #[], #argument, #completion, #full_name, #help!, #initialize_copy, #inspect, #key, #method_missing, #opt, #option, #option?, #subcommand, #to_s, #to_s_help

Constructor Details

#initialize(name = nil, &block) ⇒ Command

New Usage.



15
16
17
18
# File 'lib/clio/usage/command.rb', line 15

def initialize(name=nil, &block)
  name ||= File.basename($0)
  super(name, &block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Clio::Usage::Subcommand

Instance Method Details

#cacheObject

Cache usage into a per-user cache file for reuse. This can be used to greatly speed up tab completion.



138
139
140
# File 'lib/clio/usage/command.rb', line 138

def cache
  File.open(cache_file, 'w'){ |f| f << to_yaml }
end

#parse(argv) ⇒ Object

# Usage text.

#
def to_s
  #s = [full_name]
  s = [name]

  case options.size
  when 0
  when 1, 2, 3
    s.concat(options.collect{ |o| "[#{o.to_s.strip}]" })
  else
    s << "[switches]"
  end

# switches? vs. options

  s << arguments.join(' ') unless arguments.empty?

  case commands.size
  when 0
  when 1
    s << commands.join('')
  when 2, 3
    s << '[' + commands.join(' | ') + ']'
  else
    s << 'command'
  end

  s.flatten.join(' ')
end

# Help text.
#
def to_s_help
  s = []
  unless help.empty?
    s << help
    s << ''
  end
  s << "Usage:"
  s << "  " + to_s
  unless commands.empty?
    s << ''
    s << 'Commands:'
    s.concat(commands.collect{ |x| "  %-20s %s" % [x.key, x.help] }.sort)
  end
  unless arguments.empty?
    s << ''
    s << "Arguments:"
    s.concat(arguments.collect{ |x| "  %-20s %s" % [x, x.help] })
  end
  unless options.empty?
    s << ''
    s << 'Switches:'
    s.concat(options.collect{ |x| "  %-20s %s" % [x, x.help] })
  end
  s.flatten.join("\n")
end


131
132
133
# File 'lib/clio/usage/command.rb', line 131

def parse(argv)
  Parser.new(self, argv).parse #(argv)
end