Class: Aspera::Cli::OptionRegistry
- Inherits:
-
Object
- Object
- Aspera::Cli::OptionRegistry
- Defined in:
- lib/aspera/cli/option_registry.rb
Overview
Declared options, and their lookup by name: long, short, or unique abbreviation.
Instance Attribute Summary collapse
-
#group ⇒ String
Help section of options declared next.
-
#options ⇒ Hash{Symbol => OptionValue}
readonly
Declared options, in declaration order.
Instance Method Summary collapse
-
#add(option, short: nil) ⇒ OptionValue
Register a new option.
-
#by_long(name, allow_abbreviation: true) ⇒ OptionValue?
Find long option by exact name, or by unique abbreviation.
-
#by_short(char) ⇒ OptionValue?
Declared option, or
nil. -
#declared?(option_symbol) ⇒ Boolean
trueif option is declared. -
#fetch(option_symbol) ⇒ OptionValue
Declared option.
-
#initialize ⇒ OptionRegistry
constructor
A new instance of OptionRegistry.
Constructor Details
#initialize ⇒ OptionRegistry
Returns a new instance of OptionRegistry.
16 17 18 19 20 21 |
# File 'lib/aspera/cli/option_registry.rb', line 16 def initialize @options = {} # Short option char -> option, e.g. {'h' => help option} @short_options = {} @group = 'global' end |
Instance Attribute Details
#group ⇒ String
Returns help section of options declared next.
14 15 16 |
# File 'lib/aspera/cli/option_registry.rb', line 14 def group @group end |
#options ⇒ Hash{Symbol => OptionValue} (readonly)
Returns declared options, in declaration order.
12 13 14 |
# File 'lib/aspera/cli/option_registry.rb', line 12 def @options end |
Instance Method Details
#add(option, short: nil) ⇒ OptionValue
Register a new option
27 28 29 30 31 32 33 34 |
# File 'lib/aspera/cli/option_registry.rb', line 27 def add(option, short: nil) Aspera.assert(!@options.key?(option.option)) { "#{option.option} already declared" } option.group = @group option.short = short @options[option.option] = option @short_options[short] = option unless short.nil? option end |
#by_long(name, allow_abbreviation: true) ⇒ OptionValue?
Find long option by exact name, or by unique abbreviation.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aspera/cli/option_registry.rb', line 57 def by_long(name, allow_abbreviation: true) option = @options[name.to_sym] return option if !option.nil? || !allow_abbreviation candidates = @options.keys.select { |k| k.to_s.start_with?(name) } return if candidates.empty? Aspera.assert(candidates.length.eql?(1), type: BadArgument) do Parser.multi_choice_assert_msg("Ambiguous option: #{Parser.option_name_to_line(name)}", candidates.map { |c| Parser.option_name_to_line(c) }) end @options[candidates.first] end |
#by_short(char) ⇒ OptionValue?
Returns declared option, or nil.
50 |
# File 'lib/aspera/cli/option_registry.rb', line 50 def by_short(char) = @short_options[char] |
#declared?(option_symbol) ⇒ Boolean
Returns true if option is declared.
38 |
# File 'lib/aspera/cli/option_registry.rb', line 38 def declared?(option_symbol) = @options.key?(option_symbol) |
#fetch(option_symbol) ⇒ OptionValue
Returns declared option.
43 44 45 46 |
# File 'lib/aspera/cli/option_registry.rb', line 43 def fetch(option_symbol) Aspera.assert(@options.key?(option_symbol), type: BadArgument) { "Unknown option: #{option_symbol}" } @options[option_symbol] end |