Class: OptionsByExample

Inherits:
Object
  • Object
show all
Defined in:
lib/options_by_example.rb,
lib/options_by_example/version.rb,
lib/options_by_example/commandline_parser.rb,
lib/options_by_example/usage_specification.rb

Defined Under Namespace

Classes: CommandlineParser, PrintUsageMessage, UsageSpecification

Constant Summary collapse

VERSION =
'4.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ OptionsByExample

Returns a new instance of OptionsByExample.



18
19
20
21
22
23
# File 'lib/options_by_example.rb', line 18

def initialize(text)
  @usage_spec = UsageSpecification.new(text)

  initialize_argument_accessors
  initialize_option_accessors
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



10
11
12
# File 'lib/options_by_example.rb', line 10

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/options_by_example.rb', line 11

def options
  @options
end

#usage_specObject (readonly)

Returns the value of attribute usage_spec.



12
13
14
# File 'lib/options_by_example.rb', line 12

def usage_spec
  @usage_spec
end

Class Method Details

.read(data) ⇒ Object



14
15
16
# File 'lib/options_by_example.rb', line 14

def self.read(data)
  return new data.read
end

Instance Method Details

#expect_at_most_one_except(*extra_options) ⇒ Object



34
35
36
# File 'lib/options_by_example.rb', line 34

def expect_at_most_one_except(*extra_options)
  expect_at_most_one *(@options.keys - extra_options)
end

#expect_at_most_one_of(*mutually_exclusive_options) ⇒ Object



38
39
40
# File 'lib/options_by_example.rb', line 38

def expect_at_most_one_of(*mutually_exclusive_options)
  expect_at_most_one *mutually_exclusive_options
end

#fetch(*args, &block) ⇒ Object



53
54
55
# File 'lib/options_by_example.rb', line 53

def fetch(*args, &block)
  @arguments.fetch(*args, &block)
end

#get(name) ⇒ Object



57
58
59
# File 'lib/options_by_example.rb', line 57

def get(name)
  @arguments[name]
end

#get_at_most_one(*mutually_exclusive_options) ⇒ Object Also known as: expect_at_most_one, get_mutually_exclusive



42
43
44
45
46
47
48
# File 'lib/options_by_example.rb', line 42

def get_at_most_one(*mutually_exclusive_options)
  provided_options = @options.keys & mutually_exclusive_options
  if provided_options.length > 1
    abort "ERR: Found more than one mutually-exclusive option {#{provided_options.join ', '}}"
  end
  provided_options.first
end

#if_present(name) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/options_by_example.rb', line 61

def if_present(name)
  raise ArgumentError, 'block missing' unless block_given?

  value = @arguments[name]
  value.nil? ? value : (yield value)
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/options_by_example.rb', line 68

def include?(name)
  @options.include?(name)
end

#parse(argv) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/options_by_example.rb', line 25

def parse(argv)
  parse_without_exit argv
rescue PrintUsageMessage
  puts @usage_spec.message
  exit 0
rescue RuntimeError => err
  abort "ERR: #{err.message}"
end