Class: Aspera::Cli::Option
- Inherits:
-
Object
- Object
- Aspera::Cli::Option
- Defined in:
- lib/aspera/cli/command_line.rb
Overview
Option command line token (long or short form).
Constant Summary collapse
- NAME_SEP_LINE =
Option name separator on command line (e.g.
--option-name, the-between words) '-'- NAME_SEP_SYMBOL =
Option name separator in code/symbol (e.g.
:option_name, the_between words) '_'- VALUE_SEP =
Separator between option name and its inline value (e.g.
--opt=val, the=) '='- PREFIX =
Long-option prefix (e.g.
--opt) '--'- STOP =
When alone, stops option processing: following tokens are positional arguments
'--'
Instance Attribute Summary collapse
-
#abbreviation_of ⇒ Symbol?
Option this token was resolved to, when
nameis an abbreviation. -
#consumed ⇒ Boolean
trueonce applied to a declared option. -
#dot_path ⇒ Array<String>?
readonly
Sub-keys for dot-path notation (e.g. ["field"] for --custom.field).
-
#inline_value ⇒ String?
readonly
Value given in the same token (
--opt=valor-Oval). -
#name ⇒ String?
readonly
Option name with underscores (e.g. "log_level"), nil for short options.
-
#raw ⇒ String
readonly
Raw token as it appeared in argv (e.g. "--log-level=debug", "-Pval").
-
#short_char ⇒ String?
readonly
Single-char short option letter (e.g. "P"), nil for long options.
-
#value_token ⇒ Argument?
Next token, claimed as value when there is no inline value (
--opt valor-O val).
Class Method Summary collapse
-
.option?(token) ⇒ Boolean
trueif token is an option, i.e. -
.parse(raw) ⇒ Option
Build an Option from a raw token.
Instance Method Summary collapse
-
#initialize(raw:, name: nil, short_char: nil, dot_path: nil, inline_value: nil) ⇒ Option
constructor
A new instance of Option.
-
#inline? ⇒ Boolean
trueif value is in the same token. -
#to_s ⇒ String
Raw token, followed by its separate value if any.
-
#value ⇒ String?
Inline value, or value of next token.
Constructor Details
#initialize(raw:, name: nil, short_char: nil, dot_path: nil, inline_value: nil) ⇒ Option
Returns a new instance of Option.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/aspera/cli/command_line.rb', line 51 def initialize(raw:, name: nil, short_char: nil, dot_path: nil, inline_value: nil) @raw = raw @name = name @short_char = short_char @dot_path = dot_path @inline_value = inline_value @value_token = nil @consumed = false @abbreviation_of = nil end |
Instance Attribute Details
#abbreviation_of ⇒ Symbol?
Returns option this token was resolved to, when name is an abbreviation.
49 50 51 |
# File 'lib/aspera/cli/command_line.rb', line 49 def abbreviation_of @abbreviation_of end |
#consumed ⇒ Boolean
Returns true once applied to a declared option.
47 48 49 |
# File 'lib/aspera/cli/command_line.rb', line 47 def consumed @consumed end |
#dot_path ⇒ Array<String>? (readonly)
Returns sub-keys for dot-path notation (e.g. ["field"] for --custom.field).
41 42 43 |
# File 'lib/aspera/cli/command_line.rb', line 41 def dot_path @dot_path end |
#inline_value ⇒ String? (readonly)
Returns value given in the same token (--opt=val or -Oval).
43 44 45 |
# File 'lib/aspera/cli/command_line.rb', line 43 def inline_value @inline_value end |
#name ⇒ String? (readonly)
Returns option name with underscores (e.g. "log_level"), nil for short options.
37 38 39 |
# File 'lib/aspera/cli/command_line.rb', line 37 def name @name end |
#raw ⇒ String (readonly)
Returns raw token as it appeared in argv (e.g. "--log-level=debug", "-Pval").
35 36 37 |
# File 'lib/aspera/cli/command_line.rb', line 35 def raw @raw end |
#short_char ⇒ String? (readonly)
Returns single-char short option letter (e.g. "P"), nil for long options.
39 40 41 |
# File 'lib/aspera/cli/command_line.rb', line 39 def short_char @short_char end |
#value_token ⇒ Argument?
Returns next token, claimed as value when there is no inline value (--opt val or -O val).
45 46 47 |
# File 'lib/aspera/cli/command_line.rb', line 45 def value_token @value_token end |
Class Method Details
.option?(token) ⇒ Boolean
Returns true if token is an option, i.e. not -, --, or a negative number.
74 75 76 |
# File 'lib/aspera/cli/command_line.rb', line 74 def option?(token) token.match?(/\A-\D/) && !token.eql?(STOP) end |
.parse(raw) ⇒ Option
Build an Option from a raw token
81 82 83 84 85 86 87 88 89 |
# File 'lib/aspera/cli/command_line.rb', line 81 def parse(raw) if raw.start_with?(PREFIX) name_raw, value = raw.delete_prefix(PREFIX).split(VALUE_SEP, 2) root, *dot_path = name_raw.to_s.split(DotContainer::SEPARATOR) new(raw: raw, name: root.to_s.gsub(NAME_SEP_LINE, NAME_SEP_SYMBOL), dot_path: dot_path.empty? ? nil : dot_path, inline_value: value) else new(raw: raw, short_char: raw[1], inline_value: raw.length > 2 ? raw[2..] : nil) end end |
Instance Method Details
#inline? ⇒ Boolean
Returns true if value is in the same token.
63 |
# File 'lib/aspera/cli/command_line.rb', line 63 def inline? = !@inline_value.nil? |
#to_s ⇒ String
Returns raw token, followed by its separate value if any.
69 |
# File 'lib/aspera/cli/command_line.rb', line 69 def to_s = @value_token.nil? ? @raw : "#{@raw} #{@value_token.value}" |
#value ⇒ String?
Returns inline value, or value of next token.
66 |
# File 'lib/aspera/cli/command_line.rb', line 66 def value = @inline_value || @value_token&.value |