Class: DohApp::Options
Instance Method Summary collapse
-
#initialize(param_definition_hash_or_array, allow_unknown_options = false, explanation_text = '', unknown_options_name = 'file', allow_no_prefix = false) ⇒ Options
constructor
option_list is an array, the first element of which is the default value of the option the others are of the form with examples shown on www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html returns any non-options parameters via method varargs set allow_unknown_options to true to allow for filename specification, etc..
- #method_missing(sym, *args) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(param_definition_hash_or_array, allow_unknown_options = false, explanation_text = '', unknown_options_name = 'file', allow_no_prefix = false) ⇒ Options
option_list is an array, the first element of which is the default value of the option the others are of the form with examples shown on www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/index.html returns any non-options parameters via method varargs set allow_unknown_options to true to allow for filename specification, etc.. if the default value is :required for a field, then if that field isn’t specified, an exception will be raised
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/doh/app/options.rb', line 19 def initialize(param_definition_hash_or_array, = false, explanation_text = '', = 'file', allow_no_prefix = false) @vals = OpenStruct.new @opts = OptionParser.new @opts. = "Usage: #{$0} [options]" if explanation_text != '' @opts.separator "" @opts.separator explanation_text end @opts.separator "" @opts.separator "Specific options:" @opts.separator "" if param_definition_hash_or_array.class == Array param_definition_array = param_definition_hash_or_array param_definition_hash = param_definition_hash_or_array.inject({}) {|sum, elem| sum.merge(elem)} else param_definition_array = param_definition_hash_or_array.collect {|elem1, elem2| {elem1 => elem2}} param_definition_hash = param_definition_hash_or_array end param_definition_array.each do |elem| keyname = elem.to_a[0][1][2] if keyname =~ /^--no-/ raise "key name: #{keyname} will not behave as desired if meant to be a boolean, rename it, or pass in allow_no_prefix flag as 5th param" end end param_definition_array.each do |elem| key = elem.to_a[0][0] val = elem.to_a[0][1] default = val.shift @vals.send("#{key}=", default) val.last.insert(0, '(required) - ') if val.last.is_a?(String) && default == :required @opts.on(*val) do |optval| @vals.send("#{key}=", optval) end end @opts.separator "" @opts.separator "Common options:" @opts.separator "" @opts.on("-h", "-?", "--help", "Show this message") do puts @opts exit end varargs = [] @opts.order(ARGV) do |arg| varargs.push(arg) end @vals.varargs = varargs unset_vars = [] param_definition_hash.each do |key,val| if @vals.send(key) == :required unset_vars.push(key) end end exception = '' exception << "Required options not specified: #{unset_vars.inspect}\n" unless unset_vars.size == 0 exception << "Unknown options specified: #{varargs.inspect}\n" unless || (varargs.size == 0) exception << "You must specify #{} #{}#{ > 1 ? 's' : ''}" if .class == Fixnum && varargs.size != if .class == Range if (.min == .max) if (varargs.size < .min) exception << "You must specify at least #{.min} #{}#{.min > 1 ? 's' : ''}" end elsif !.include?(varargs.size) exception << "You must specify between #{.min} and #{.max} #{}s" end end if exception != '' puts @opts raise exception end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
99 100 101 |
# File 'lib/doh/app/options.rb', line 99 def method_missing(sym, *args) @vals.send(sym, *args) end |
Instance Method Details
#to_s ⇒ Object
103 104 105 |
# File 'lib/doh/app/options.rb', line 103 def to_s @opts.to_s end |