Class: Aspera::Cli::Option

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw:, name:, short_char:, dot_path:, value:, has_value:) ⇒ Option

Returns a new instance of Option.

Parameters:

  • raw (String)

    full raw token

  • name (String, nil)

    option name (underscored, no prefix)

  • short_char (String, nil)

    single-char short letter

  • dot_path (Array<String>, nil)

    dot-path sub-keys, or nil

  • value (String, nil)

    inline value

  • has_value (Boolean)

    whether an inline value separator was present



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_pathArray<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).

Returns:

  • (Array<String>, nil)

    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_valueBoolean (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.

Returns:

  • (Boolean)

    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

#nameString? (readonly)

Returns option name with underscores (e.g. "log_level", "custom"); nil for short options.

Returns:

  • (String, nil)

    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

#rawString (readonly)

Returns raw token as it appeared in argv (e.g. "--log-level=debug", "-Pval").

Returns:

  • (String)

    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_charString? (readonly)

Returns single-char short option letter (e.g. "P"), nil for long options.

Returns:

  • (String, nil)

    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

#valueString? (readonly)

Returns inline value string, or nil if no value was provided inline.

Returns:

  • (String, nil)

    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 --)

Parameters:

  • raw (String)

    e.g. "--log-level=debug" or "--custom.field" or "--log-level"



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 --)

Parameters:

  • raw (String)

    e.g. "-P", "-Pval", "-h"



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_sObject



357
# File 'lib/aspera/cli/parser.rb', line 357

def to_s = @raw