Module: OptionParserGenerator

Defined in:
lib/optsparser_generator.rb

Overview

Small lib for generating an OptionParser from an OpenStruct

Defined Under Namespace

Modules: OptParsePatch Classes: OptionCollision, WrongArgumentType

Class Method Summary collapse

Class Method Details

.[](ostruct, **options) ⇒ Object

TODO:

write documentation :(

TODO:

split this up

Does the magic

Options Hash (**options):

  • :ignore_collisions (Boolean)

    ignore bool key collisions see OptionCollision


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/optsparser_generator.rb', line 34

def self.[](ostruct, **options)
  defaults = handle_arguments(ostruct)

  optparser = OptionParser.new do |opts|
    defaults.each_pair do |key, val|
      trigger = key.to_s.tr('_', '-')
      next if trigger.end_with?('--help', '--values', '--short', '--class')

      help = "#{defaults["#{key}__help"]} (Default: #{val})"
      values = defaults["#{key}__values"] || []
      short = defaults["#{key}__short"] || ''
      arguments = []
      arguments << help
      arguments << "-#{short}" unless short.empty?
      case val
      when FalseClass, TrueClass
        if trigger.start_with?('no-')
          trigger[0..2] = ''
          if defaults.each_pair.map { |v| v.first.to_s }.include?(trigger) && !options[:ignore_collisions]
            raise OptionCollision, "on #{key}"
          end
        end
        opts.on("--[no-]#{trigger}", *arguments) do |b|
          out = opts.instance_variable_get(:@out)
          out[key] =
            if key.to_s.start_with?('no_')
              !b
            else
              b
            end
        end
      else
        arguments.push defaults["#{key}__class"] || (val.class.equal?(Fixnum) ? Integer : val.class)
        arguments << values if values.any?
        opts.on("--#{trigger}=ARG", *arguments) do |str|
          out = opts.instance_variable_get(:@out)
          out[key] = str
        end
      end
    end

    opts.on('-h', '--help') do
      puts opts
      exit
    end
  end

  # add default values
  optparser.instance_variable_set(:@defaults, defaults)
  optparser.extend(OptParsePatch)
  optparser
end

.handle_arguments(ostruct) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO:

preprocess data here instead of doing it adhoc

TODO:

raise more exceptions

Does some sanity checks and prepares the OpenStruct


20
21
22
23
24
25
# File 'lib/optsparser_generator.rb', line 20

def self.handle_arguments(ostruct)
  unless ostruct.is_a?(OpenStruct)
    raise WrongArgumentType, 'needs an OpenStruct'
  end
  ostruct.dup.freeze # not needed but makes development easier
end

.parse(ostruct, argv = nil, **opt) ⇒ OpenStruct

Shorthand when parsing is only needed once.

Generates an OptionParser and calls parse on it

See Also:


110
111
112
# File 'lib/optsparser_generator.rb', line 110

def self.parse(ostruct, argv = nil, **opt)
  self[ostruct, opt].parse(argv)
end

.parse!(ostruct, argv = ARGV, **opt) ⇒ OpenStruct

Same as parse, removes parsed elements from argv

See Also:


117
118
119
# File 'lib/optsparser_generator.rb', line 117

def self.parse!(ostruct, argv = ARGV, **opt)
  self[ostruct, opt].parse!(argv)
end