Class: OptionGrouper

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/optiongrouper.rb

Overview

A basic command line option parser with group support.

Defined Under Namespace

Classes: Group

Constant Summary collapse

VERSION =
File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ OptionGrouper

Initialize an OptionGrouper. You can configure it using the yielded block.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/optiongrouper.rb', line 93

def initialize &blk
  @groups = {}
  @on_version = :exit
  @on_help = :exit
  @on_invalid_parameter = :exit
  @on_ambigous_parameter = :exit
  @on_not_argument = :exit

  run = Proc.new { show_help }
  group do
    header 'General options'
    opt :help, 'Show this message', :short => 'h', :on => run
  end

  invoke &blk if blk
end

Instance Attribute Details

#resultHash{Symbol => Hash{Symbol => Object}} (readonly)

Hash of parsed results. Hash keys are the group names, values are another hashes, where key is the option name and value is the parsed value.

Returns:

  • (Hash{Symbol => Hash{Symbol => Object}})


206
207
208
# File 'lib/optiongrouper.rb', line 206

def result
  @result
end

Instance Method Details

#group(name = :default) { ... } ⇒ Group

Define a new group, if needed, then invoke it.

Parameters:

  • name (Symbol) (defaults to: :default)

    name of the group

Yields:

  • a configuration block

Returns:

  • (Group)

    the defined group



171
172
173
174
175
# File 'lib/optiongrouper.rb', line 171

def group name = :default, &blk
  @groups[name] ||= Group.new(name)
  @groups[name].invoke &blk
  @groups[name]
end

#header(str) ⇒ void

This method returns an undefined value.

Sets header printed when help is requested.

Parameters:

  • str (#to_s)

    header message



163
164
165
# File 'lib/optiongrouper.rb', line 163

def header str
  @header = str
end

#invoke(&blk) ⇒ Object

Yield the configurator block again.



111
112
113
# File 'lib/optiongrouper.rb', line 111

def invoke &blk
  Blockenspiel.invoke blk, self
end

#on_ambigous_parameter(to) ⇒ Object

What to do after an ambigous parameter. Takes same options as #on_invalid_parameter.



139
140
141
# File 'lib/optiongrouper.rb', line 139

def on_ambigous_parameter to
  @on_ambigous_parameter = to
end

#on_help(res) ⇒ Object

What to do after printing help. Takes same options as #on_version.



127
128
129
# File 'lib/optiongrouper.rb', line 127

def on_help res
  @on_help = res
end

#on_invalid_parameter(to) ⇒ Object

What to do after an invalid parameter. Takes the same options as #on_version, except you can also use :raise to raise an exception.



133
134
135
# File 'lib/optiongrouper.rb', line 133

def on_invalid_parameter to
  @on_invalid_parameter = to
end

#on_not_argument(to) ⇒ Object

What to do with a non-option string. Takes same options as #on_invalid_parameter.



145
146
147
# File 'lib/optiongrouper.rb', line 145

def on_not_argument to
  @on_not_argument = to
end

#on_version(res) ⇒ void

This method returns an undefined value.

What to do after printing version.

  • :exit: call exit to quit the program.
  • :continue: continue processing
  • :stop: stop processing
  • a Proc: call it

Parameters:

  • res (Symbol, Proc)

    do this.



122
123
124
# File 'lib/optiongrouper.rb', line 122

def on_version res
  @on_version = res
end

#parse(args = ARGV) ⇒ Hash

Parse command line arguments

Parameters:

  • args (Array<String>) (defaults to: ARGV)

    list of command line arguments.

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/optiongrouper.rb', line 180

def parse args = ARGV
  init_parse

  catch(:stop) do
    while a = args.shift
      if a == '--'
        break
      elsif a =~ /^--[^-]/
        handle_long args, a
      elsif a =~ /^-[^-]/
        handle_short args, a
      else
        @ignored << a
        on_error @on_not_argument, "`#{a}' is not an argument."
      end
    end
  end
  @result
ensure
  args.unshift *@ignored
end

#stopObject

Stop argument processing (call it in callbacks)



209
210
211
# File 'lib/optiongrouper.rb', line 209

def stop
  throw :stop
end

#version(version) ⇒ void

This method returns an undefined value.

Sets the version of the program

Parameters:

  • version (#to_s)

    program name and version.



152
153
154
155
156
157
158
# File 'lib/optiongrouper.rb', line 152

def version version
  @version = version
  run = Proc.new { show_version }
  group :default do
    opt :version, 'Show version', :on => run
  end
end