Class: Aspera::Cli::Option::Registry
- Inherits:
-
Object
- Object
- Aspera::Cli::Option::Registry
- 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 => Option::Value}
readonly
Declared options, in declaration order.
Instance Method Summary collapse
-
#add(option, short: nil) ⇒ Option::Value
Register a new option.
-
#by_long(name, allow_abbreviation: true) ⇒ Option::Value?
Find long option by exact name, or by unique abbreviation.
-
#by_short(char) ⇒ Option::Value?
Declared option, or
nil. -
#declared?(option_symbol) ⇒ Boolean
trueif option is declared. -
#fetch(option_symbol) ⇒ Option::Value
Declared option.
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
20 21 22 23 24 25 |
# File 'lib/aspera/cli/option/registry.rb', line 20 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.
18 19 20 |
# File 'lib/aspera/cli/option/registry.rb', line 18 def group @group end |
#options ⇒ Hash{Symbol => Option::Value} (readonly)
Returns declared options, in declaration order.
16 17 18 |
# File 'lib/aspera/cli/option/registry.rb', line 16 def @options end |
Instance Method Details
#add(option, short: nil) ⇒ Option::Value
Register a new option
31 32 33 34 35 36 37 38 |
# File 'lib/aspera/cli/option/registry.rb', line 31 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) ⇒ Option::Value?
Find long option by exact name, or by unique abbreviation.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/aspera/cli/option/registry.rb', line 61 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) ⇒ Option::Value?
Returns declared option, or nil.
54 |
# File 'lib/aspera/cli/option/registry.rb', line 54 def by_short(char) = @short_options[char] |
#declared?(option_symbol) ⇒ Boolean
Returns true if option is declared.
42 |
# File 'lib/aspera/cli/option/registry.rb', line 42 def declared?(option_symbol) = @options.key?(option_symbol) |
#fetch(option_symbol) ⇒ Option::Value
Returns declared option.
47 48 49 50 |
# File 'lib/aspera/cli/option/registry.rb', line 47 def fetch(option_symbol) Aspera.assert(@options.key?(option_symbol), type: BadArgument) { "Unknown option: #{option_symbol}" } @options[option_symbol] end |