Class: Aspera::Cli::Option
- Inherits:
-
Object
- Object
- Aspera::Cli::Option
- Defined in:
- lib/aspera/cli/parser.rb
Overview
Represents a parsed CLI option token (long or short form). Pre-computed at argv-scan time; resolution against @declared_options happens later in parse_options!
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) '--'
Instance Attribute Summary collapse
-
#dot_path ⇒ Array<String>?
readonly
Sub-keys for dot-path notation (e.g. ["field"] for --custom.field), nil when name is the full option name (no dot).
-
#has_value ⇒ Boolean
readonly
True if
=(long) or glued value (short) was present in the raw token; false means no inline separator — value may come from the next argv token. -
#name ⇒ String?
readonly
Option name with underscores (e.g. "log_level", "custom"); 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 ⇒ String?
readonly
Inline value string, or nil if no value was provided inline.
Class Method Summary collapse
-
.from_long(raw) ⇒ Object
Build an Option from a raw long-option token (starts with
--). -
.from_short(raw) ⇒ Object
Build an Option from a raw short-option token (starts with
-but not--).
Instance Method Summary collapse
-
#initialize(raw:, name:, short_char:, dot_path:, value:, has_value:) ⇒ Option
constructor
A new instance of Option.
- #to_s ⇒ Object
Constructor Details
#initialize(raw:, name:, short_char:, dot_path:, value:, has_value:) ⇒ Option
Returns a new instance of Option.
348 349 350 351 352 353 354 355 |
# File 'lib/aspera/cli/parser.rb', line 348 def initialize(raw:, name:, short_char:, dot_path:, value:, has_value:) @raw = raw @name = name @short_char = short_char @dot_path = dot_path @value = value @has_value = has_value end |
Instance Attribute Details
#dot_path ⇒ Array<String>? (readonly)
Returns sub-keys for dot-path notation (e.g. ["field"] for --custom.field), nil when name is the full option name (no dot).
335 336 337 |
# File 'lib/aspera/cli/parser.rb', line 335 def dot_path @dot_path end |
#has_value ⇒ Boolean (readonly)
Returns true if = (long) or glued value (short) was present in the raw token;
false means no inline separator — value may come from the next argv token.
340 341 342 |
# File 'lib/aspera/cli/parser.rb', line 340 def has_value @has_value end |
#name ⇒ String? (readonly)
Returns option name with underscores (e.g. "log_level", "custom"); nil for short options.
330 331 332 |
# File 'lib/aspera/cli/parser.rb', line 330 def name @name end |
#raw ⇒ String (readonly)
Returns raw token as it appeared in argv (e.g. "--log-level=debug", "-Pval").
328 329 330 |
# File 'lib/aspera/cli/parser.rb', line 328 def raw @raw end |
#short_char ⇒ String? (readonly)
Returns single-char short option letter (e.g. "P"), nil for long options.
332 333 334 |
# File 'lib/aspera/cli/parser.rb', line 332 def short_char @short_char end |
#value ⇒ String? (readonly)
Returns inline value string, or nil if no value was provided inline.
337 338 339 |
# File 'lib/aspera/cli/parser.rb', line 337 def value @value end |
Class Method Details
.from_long(raw) ⇒ Object
Build an Option from a raw long-option token (starts with --)
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/aspera/cli/parser.rb', line 362 def from_long(raw) without_prefix = raw.delete_prefix(PREFIX) eq_idx = without_prefix.index(VALUE_SEP) if eq_idx name_raw = without_prefix[0, eq_idx] value = without_prefix[eq_idx + 1..] has_value = true else name_raw = without_prefix value = nil has_value = false end parts = name_raw.split(DotContainer::SEPARATOR) root = parts.shift.gsub(NAME_SEP_LINE, NAME_SEP_SYMBOL) dot_path = parts.empty? ? nil : parts new(raw: raw, name: root, short_char: nil, dot_path: dot_path, value: value, has_value: has_value) end |
.from_short(raw) ⇒ Object
Build an Option from a raw short-option token (starts with - but not --)
382 383 384 385 386 387 388 389 |
# File 'lib/aspera/cli/parser.rb', line 382 def from_short(raw) short_char = raw[1] if raw.length > 2 new(raw: raw, name: nil, short_char: short_char, dot_path: nil, value: raw[2..], has_value: true) else new(raw: raw, name: nil, short_char: short_char, dot_path: nil, value: nil, has_value: false) end end |
Instance Method Details
#to_s ⇒ Object
357 |
# File 'lib/aspera/cli/parser.rb', line 357 def to_s = @raw |