Class: Hiiro::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/options.rb

Defined Under Namespace

Classes: Args, Definition

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Options

Returns a new instance of Options.



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

def initialize(&block)
  base_initialize(&block)
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



3
4
5
# File 'lib/hiiro/options.rb', line 3

def definitions
  @definitions
end

Class Method Details

.new(args = nil, &block) ⇒ Object

Support both: new(&block) for setup, or new(args, &block) for parse



14
15
16
17
18
19
20
21
22
# File 'lib/hiiro/options.rb', line 14

def self.new(args = nil, &block)
  instance = allocate
  instance.send(:base_initialize, &block)
  if args
    instance.parse!(args)
  else
    instance
  end
end

.parse(args, &block) ⇒ Object



5
6
7
# File 'lib/hiiro/options.rb', line 5

def self.parse(args, &block)
  new(&block).parse!(args)
end

.setup(&block) ⇒ Object



9
10
11
# File 'lib/hiiro/options.rb', line 9

def self.setup(&block)
  new(&block)
end

Instance Method Details

#flag(name, long: nil, short: nil, default: false, desc: nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/hiiro/options.rb', line 52

def flag(name, long: nil, short: nil, default: false, desc: nil)
  defn = Definition.new(name, long: long, short: short, type: :flag, default: default, desc: desc)
  deconflict_short(short) if short
  @definitions[name.to_sym] = defn
  self
end

#help_textObject



34
35
36
37
38
39
40
41
42
# File 'lib/hiiro/options.rb', line 34

def help_text
  lines = []
  @definitions.each do |name, defn|
    next if name == :help
    lines << defn.usage_line
  end
  lines << @definitions[:help].usage_line
  lines.join("\n")
end

#hintObject



44
45
46
47
48
49
50
# File 'lib/hiiro/options.rb', line 44

def hint
  @definitions
    .reject { |k, _| k == :help }
    .map { |_, d| d.flag? ? d.long_form : "#{d.long_form} <val>" }
    .map { |s| "[#{s}]" }
    .join(' ')
end

#mutual_exclusion(*names) ⇒ Object

Declare a mutual exclusion group with star topology. The first name is the hub; the rest are spokes.

Setting the hub   clears all spokes.
Setting a spoke   clears only the hub.
Spokes can still be combined with each other freely.

Two-member groups are fully symmetric (hub == spoke). Last flag encountered in argv always wins.



73
74
75
76
77
# File 'lib/hiiro/options.rb', line 73

def mutual_exclusion(*names)
  @mutex_groups ||= []
  @mutex_groups << names.map(&:to_sym)
  self
end

#option(name, long: nil, short: nil, type: :string, default: nil, desc: nil, multi: false, flag_ifs: []) ⇒ Object



59
60
61
62
63
64
# File 'lib/hiiro/options.rb', line 59

def option(name, long: nil, short: nil, type: :string, default: nil, desc: nil, multi: false, flag_ifs: [])
  defn = Definition.new(name, long: long, short: short, type: type, default: default, desc: desc, multi: multi, flag_ifs: Array(flag_ifs))
  deconflict_short(short) if short
  @definitions[name.to_sym] = defn
  self
end

#parse(args) ⇒ Object



97
98
99
# File 'lib/hiiro/options.rb', line 97

def parse(args)
  Args.new(@definitions, args.flatten.compact, mutex_groups: @mutex_groups || [])
end

#parse!(args) ⇒ Object



101
102
103
# File 'lib/hiiro/options.rb', line 101

def parse!(args)
  parse(args)
end

#select(names) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/hiiro/options.rb', line 88

def select(names)
  subset = self.class.setup {}
  names.each do |name|
    defn = @definitions[name.to_sym]
    subset.definitions[name.to_sym] = defn if defn
  end
  subset
end