Class: Lab42::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/lab42/options.rb,
lib/lab42/options/parser.rb,
lib/lab42/options/version.rb,
lib/lab42/options/validator.rb,
lib/lab42/options/error_issuer.rb,
lib/lab42/options/array_helpers.rb,
lib/lab42/options/default_helpers.rb

Direct Known Subclasses

PermissiveOptions

Defined Under Namespace

Modules: ArrayHelpers, DefaultHelpers Classes: ErrorIssuer, Parser, Validator

Constant Summary collapse

VERSION =
"0.5.3"
ParameterError =
Class.new ArgumentError
Identity =
->(x){x}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



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

def args
  @args
end

#strict_modeObject (readonly)

Returns the value of attribute strict_mode.



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

def strict_mode
  @strict_mode
end

#yaml_fileObject (readonly)

Returns the value of attribute yaml_file.



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

def yaml_file
  @yaml_file
end

Instance Method Details

#defaultsObject



19
20
21
22
# File 'lib/lab42/options.rb', line 19

def defaults
  @__defaults__ ||=
    @registered.select{|_,v| v != :required}
end

#define_help(txt) ⇒ Object



14
15
16
17
# File 'lib/lab42/options.rb', line 14

def define_help txt
  @defined_help_text = txt
  self
end

#define_help_for(opt, txt = nil, &blk) ⇒ Object



24
25
26
27
# File 'lib/lab42/options.rb', line 24

def define_help_for opt, txt=nil, &blk
  @help_text_for_option[opt] = txt||blk.(defaults.fetch(opt))
  self
end

#error_mode?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/lab42/options.rb', line 77

def error_mode?
  strict? && !warning_mode?
end

#get_help_textObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lab42/options.rb', line 29

def get_help_text
  (
  [ @defined_help_text ] +
  required_options.map do | ro |
    "#{ro}: #{@help_text_for_option.fetch(ro, :required)}"
  end +
  defaults.map do | d,v | 
    "#{d}: defaults to #{@help_text_for_option.fetch(d,v.inspect)}"
  end
  ).compact.join("\n")
end

#parse(*args) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lab42/options.rb', line 41

def parse *args
  args = args.first if Array === args.first
  @args = args
  @parsed = Lab42::Options::Parser.new.parse( self, args )

  return if help_asked?

  validate!
  
  set_defaults
  
  OpenStruct.new( @parsed ).forwarding_to :kwds
end

#read_from(file_sym_or_hash) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/lab42/options.rb', line 55

def read_from file_sym_or_hash
  case file_sym_or_hash
  when String
    read_from_file file_sym_or_hash
  when Symbol
    read_from_parameterized_file file_sym_or_hash => nil
  else
    read_from_parameterized_file file_sym_or_hash
  end
end

#strict(new_mode = :errors) ⇒ Object



66
67
68
69
# File 'lib/lab42/options.rb', line 66

def strict(new_mode=:errors)
  @strict_mode = new_mode
  self
end

#strict?Boolean

Returns:

  • (Boolean)


71
# File 'lib/lab42/options.rb', line 71

def strict?; !!strict_mode end

#warning_mode?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/lab42/options.rb', line 73

def warning_mode?
  strict? && /warnings\z/ === strict_mode
end