Class: Strop::Result
- Inherits:
-
Object
- Object
- Strop::Result
- Defined in:
- lib/imsg-grep/utils/strop_utils.rb
Instance Method Summary collapse
-
#compact_singles!(*labels) ⇒ Object
Compacts duplicate single-occurrence options by keeping only the last occurrence.
-
#disallow_empty(*labels) ⇒ Object
Error when –opt= (empty value, like –opt=”) Does not affect nils (–opt, no arg) when called with no args, affects all opts.
-
#incompatible(*groups) ⇒ Object
Ensures that options from different groups are not used together.
-
#standalone(label) ⇒ Object
Ensures that the specified option is used alone, without any other options.
Instance Method Details
#compact_singles!(*labels) ⇒ Object
Compacts duplicate single-occurrence options by keeping only the last occurrence. Alters the result set in-place. Issues a warning when duplicates are found and removed. Example:
$ cmd -a1 -b2 -a3 -b4 -x
> result.compact_singles!(:a, :b)
# result keeps only -a3 -b4 -x
35 36 37 38 39 40 41 |
# File 'lib/imsg-grep/utils/strop_utils.rb', line 35 def compact_singles!(*labels) labels.flatten.filter_map{ (os = opts[[it]]).size > 1 and [it, os] }.each do |label, opts| names = opts.map(&:_name).uniq.join(", ") warn! "multiple #{names} options specified (last takes precedence)" replace self - opts[...-1] end end |
#disallow_empty(*labels) ⇒ Object
Error when –opt= (empty value, like –opt=”) Does not affect nils (–opt, no arg) when called with no args, affects all opts
46 47 48 49 50 51 |
# File 'lib/imsg-grep/utils/strop_utils.rb', line 46 def disallow_empty(*labels) labels = labels.flatten.map{ Strop.name_from_symbol it } candidates = labels.empty? ? opts : labels.flat_map{ opts[[it]] } opt = candidates.find{ it.value == "" } or return raise OptionError, "value for #{opt._name} cannot be empty" end |
#incompatible(*groups) ⇒ Object
Ensures that options from different groups are not used together. Each group represents a set of related options, the groups are mutually exclusive. Examples:
result.incompatible(:verbose, :quiet) # --verbose and --quiet can't be used together
result.incompatible([:json, :pretty], :binary, [:xml, :doctype]) # options from each group don't mix with from others
raises OptionError if options from different groups are used together
21 22 23 24 25 26 |
# File 'lib/imsg-grep/utils/strop_utils.rb', line 21 def incompatible(*groups) labels = opts.map(&:label) conflicts = groups.map{ [*it].map{ Strop.name_from_symbol it } & labels }.reject(&:empty?) return unless conflicts.size > 1 raise OptionError, "cannot use together: #{conflicts.flatten.map{self[it]._name}.join(', ')}" end |
#standalone(label) ⇒ Object
Ensures that the specified option is used alone, without any other options. Raises an OptionError if the label is present alongside other options. Example: result.standalone(:help) # Ensures –help cannot be used with other options raises OptionError if the label is present alongside other options
9 10 11 12 13 |
# File 'lib/imsg-grep/utils/strop_utils.rb', line 9 def standalone(label) labels = opts.map(&:label) return unless labels.include?(label) && (labels - [label]).any? raise OptionError, "cannot use #{self[label]._name} with other options" end |