Class: Clio::Usage::Main

Inherits:
Command show all
Defined in:
lib/clio/usage/main.rb

Overview

Main

Main specifies the toplevel “Command”.

Instance Attribute Summary

Attributes inherited from Command

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

Instance Method Summary collapse

Methods inherited from Command

#===, #[], #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) ⇒ Main

New Usage.



15
16
17
18
# File 'lib/clio/usage/main.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::Command

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/main.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/main.rb', line 131

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