Class: Sprinkles::Opts::GetOpt
- Inherits:
-
Object
- Object
- Sprinkles::Opts::GetOpt
- Extended by:
- T::Helpers, T::Sig
- Defined in:
- lib/sprinkles/opts.rb
Defined Under Namespace
Classes: Option
Class Method Summary collapse
- .const(name, type, short: '', long: '', factory: nil, placeholder: '', description: '', without_accessors: true) ⇒ Object
- .convert_str(value, type) ⇒ Object
- .decorator(*rest) ⇒ Object
- .parse(argv = ARGV) ⇒ Object
- .program_name ⇒ Object
Class Method Details
.const(name, type, short: '', long: '', factory: nil, placeholder: '', description: '', without_accessors: true) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/sprinkles/opts.rb', line 101 def self.const( name, type, short: '', long: '', factory: nil, placeholder: '', description: '', without_accessors: true ) # we don't want to let the user pass in nil explicitly, so the # default values here are all '' instead, but we will treat '' # as if the argument was not provided short = nil if short.empty? long = nil if long.empty? placeholder = nil if placeholder.empty? opt = Option.new( name: name, type: type, short: short, long: long, factory: factory, placeholder: placeholder, description: description ) validate!(opt) fields << opt self.define_method(name) { instance_variable_get("@#{name}") } end |
.convert_str(value, type) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/sprinkles/opts.rb', line 200 def self.convert_str(value, type) type = type.raw_type if type.is_a?(T::Types::Simple) if type.is_a?(T::Types::Union) # Right now, the assumption is that this is mostly used for # `T.nilable`, but with a bit of work we maybe could support # other kinds of unions possible_types = type.types.to_set - [T::Utils.coerce(NilClass)] raise 'TODO: generic union types' if possible_types.size > 1 convert_str(value, possible_types.first) elsif type.is_a?(Class) && type < T::Enum type.deserialize(value) elsif type == String value elsif type == Symbol value.to_sym elsif type == Integer value.to_i elsif type == Float value.to_f else raise "Don't know how to convert a string to #{type}" end end |
.decorator(*rest) ⇒ Object
80 |
# File 'lib/sprinkles/opts.rb', line 80 def self.decorator(*rest); end |
.parse(argv = ARGV) ⇒ Object
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/sprinkles/opts.rb', line 358 def self.parse(argv=ARGV) # we're going to destructively modify this argv = argv.clone values = T::Hash[Symbol, T::Array[String]].new parser = OptionParser.new do |opts| @opts = T.let(opts, T.nilable(OptionParser)) opts. = "Usage: #{program_name} #{cmdline}" opts.on('-h', '--help', 'Print this help') do usage! end fields.each do |field| next if field.positional? opts.on(*field.optparse_args) do |v| if field.repeated? (values[field.name] ||= []) << v else values[field.name] = [v] end end end end.parse!(argv) values.merge!(match_positional_fields(argv)) build_config(values) end |
.program_name ⇒ Object
16 17 18 |
# File 'lib/sprinkles/opts.rb', line 16 def self.program_name $PROGRAM_NAME end |