Class: Transat::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/transat/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Parser

Returns a new instance of Parser.



62
63
64
65
66
67
68
69
# File 'lib/transat/parser.rb', line 62

def initialize(&block)
  @valid_options, @received_options, @commands = [], {}, {}
  @option_parser = OptionParser.new

  command(:help, Transat::HelpCommand)
  command(:version, Transat::VersionCommand)
  instance_eval(&block) if block_given?
end

Class Method Details

.parse_and_execute(args = ARGV, &block) ⇒ Object



185
186
187
# File 'lib/transat/parser.rb', line 185

def self.parse_and_execute(args=ARGV, &block)
  self.new(&block).parse_and_execute(args)
end

Instance Method Details

#command(name, klass, options = {}) ⇒ Object



97
98
99
# File 'lib/transat/parser.rb', line 97

def command(name, klass, options={})
  @commands[name.to_s] = options.merge(:class => klass)
end

#execute(command, non_options) ⇒ Object

Raises:



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/transat/parser.rb', line 125

def execute(command, non_options)
  found = false
  @commands.each do |command_name, options|
    command_klass = options[:class]
    aliases = [command_name]
    aliases += command_klass.aliases if command_klass.respond_to?(:aliases)
    return command_klass.new(non_options, @received_options).run if aliases.include?(command)
  end

  raise UnknownCommand.new(command, self)
end

#option(name, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/transat/parser.rb', line 71

def option(name, options={})
  options[:long] = name.to_s.gsub("_", "-") unless options[:long]
  @valid_options << name
  @received_options[name] = nil

  opt_args = []
  opt_args << "-#{options[:short]}" if options.has_key?(:short)
  opt_args << "--#{options[:long] || name}"
  opt_args << "=#{options[:param_name]}" if options.has_key?(:param_name)
  opt_args << options[:message]
  case options[:type]
  when :int, :integer
    opt_args << Integer
  when :float
    opt_args << Float
  when nil
    # NOP
  else
    raise ArgumentError, "Option #{name} has a bad :type parameter: #{options[:type].inspect}"
  end

  @option_parser.on(*opt_args.compact) do |value|
    @received_options[name] = value
  end
end

#parse(args) ⇒ Object

Raises:



118
119
120
121
122
123
# File 'lib/transat/parser.rb', line 118

def parse(args)
  non_options = @option_parser.parse(args)
  command = non_options.shift
  raise NoCommandGiven unless command
  return command, non_options
end

#parse_and_execute(args = ARGV) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/transat/parser.rb', line 101

def parse_and_execute(args=ARGV)
  begin
    command, non_options = parse(args)
    execute(command, non_options)
  rescue HelpNeeded
    $stderr.puts usage($!.command)
    exit 1
  rescue VersionNeeded
    puts "#{program_name} #{version}"
    exit 0
  rescue NoCommandGiven, UnknownOptions, UnknownCommand
    $stderr.puts "ERROR: #{$!.message}"
    $stderr.puts usage($!.respond_to?(:command) ? $!.command : nil)
    exit 1
  end
end

#program_name(value = nil) ⇒ Object



173
174
175
# File 'lib/transat/parser.rb', line 173

def program_name(value=nil)
  value ? @program_name = value : @program_name
end

#usage(command = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/transat/parser.rb', line 137

def usage(command=nil)
  message = []

  if command then
    command_klass = @commands[command][:class]
    help =
      if command_klass.respond_to?(:aliases) then
        "#{command} (#{command_klass.aliases.join(", ")})"
      else
        "#{command}"
      end
    help = "#{help}: #{command_klass.help}" if command_klass.respond_to?(:help)
    message << help
    message << command_klass.detailed_help if command_klass.respond_to?(:detailed_help)
    message << ""
    message << "Valid options:"
    @option_parser.summarize(message)
  else
    message << "usage: #{program_name.downcase} <SUBCOMMAND> [OPTIONS] [ARGS...]"
    message << "Type '#{program_name.downcase} help <SUBCOMMAND>' for help on a specific subcommand."
    message << "Type '#{program_name.downcase} version' to get this program's version."
    message << ""
    message << "Available subcommands are:"
    @commands.sort.each do |command, options|
      command_klass = options[:class]
      if command_klass.respond_to?(:aliases) then
        message << "  #{command} (#{command_klass.aliases.join(", ")})"
      else
        message << "  #{command}"
      end
    end
  end

  message.map {|line| line.chomp}.join("\n")
end

#version(value = nil) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/transat/parser.rb', line 177

def version(value=nil)
  if value then
    @version = value.respond_to?(:join) ? value.join(".") : value
  else
    @version
  end
end