Class: Aspera::Cli::CommandLine::Option

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw:, name: nil, short_char: nil, dot_path: nil, inline_value: nil) ⇒ Option

Returns a new instance of Option.



64
65
66
67
68
69
70
71
72
73
# File 'lib/aspera/cli/command_line.rb', line 64

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.

Returns:

  • (Symbol, nil) —

    option this token was resolved to, when name is an abbreviation



62
63
64
# File 'lib/aspera/cli/command_line.rb', line 62

def abbreviation_of
  @abbreviation_of
end

#consumed ⇒ Boolean

Returns true once applied to a declared option.

Returns:

  • (Boolean) —

    true once applied to a declared option



60
61
62
# File 'lib/aspera/cli/command_line.rb', line 60

def consumed
  @consumed
end

#dot_path ⇒ Array<String>? (readonly)

Returns sub-keys for dot-path notation (e.g. ["field"] for --custom.field).

Returns:

  • (Array<String>, nil) —

    sub-keys for dot-path notation (e.g. ["field"] for --custom.field)



54
55
56
# File 'lib/aspera/cli/command_line.rb', line 54

def dot_path
  @dot_path
end

#inline_value ⇒ String? (readonly)

Returns value given in the same token (--opt=val or -Oval).

Returns:

  • (String, nil) —

    value given in the same token (--opt=val or -Oval)



56
57
58
# File 'lib/aspera/cli/command_line.rb', line 56

def inline_value
  @inline_value
end

#name ⇒ String? (readonly)

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

Returns:

  • (String, nil) —

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



50
51
52
# File 'lib/aspera/cli/command_line.rb', line 50

def name
  @name
end

#raw ⇒ String (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")



48
49
50
# File 'lib/aspera/cli/command_line.rb', line 48

def raw
  @raw
end

#short_char ⇒ String? (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



52
53
54
# File 'lib/aspera/cli/command_line.rb', line 52

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

Returns:

  • (Argument, nil) —

    next token, claimed as value when there is no inline value (--opt val or -O val)



58
59
60
# File 'lib/aspera/cli/command_line.rb', line 58

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.

Parameters:

  • token (String) —

    command line token

Returns:

  • (Boolean) —

    true if token is an option, i.e. not -, --, or a negative number



90
91
92
# File 'lib/aspera/cli/command_line.rb', line 90

def option?(token)
  token.match?(/\A-\D/) && !token.eql?(STOP)
end

.parse(raw) ⇒ Option

Build an Option from a raw token

Parameters:

  • raw (String) —

    e.g. "--log-level=debug", "--custom.field", "-P", "-Pval"

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/aspera/cli/command_line.rb', line 97

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.

Returns:

  • (Boolean) —

    true if value is in the same token



76
# File 'lib/aspera/cli/command_line.rb', line 76

def inline? = !@inline_value.nil?

#raw_hidden ⇒ String

Returns raw token, with declared secret value hidden (logs, messages).

Returns:

  • (String) —

    raw token, with declared secret value hidden (logs, messages)



82
# File 'lib/aspera/cli/command_line.rb', line 82

def raw_hidden = inline? ? "#{@raw.delete_suffix(@inline_value)}#{Secret::Hider.instance.hidden_copy(@inline_value)}" : @raw

#to_s ⇒ String

Returns raw token, followed by its separate value if any, with declared secret value hidden (logs, messages).

Returns:

  • (String) —

    raw token, followed by its separate value if any, with declared secret value hidden (logs, messages)



85
# File 'lib/aspera/cli/command_line.rb', line 85

def to_s = @value_token.nil? ? raw_hidden : "#{@raw} #{Secret::Hider.instance.hidden_copy(@value_token.value)}"

#value ⇒ String?

Returns inline value, or value of next token.

Returns:

  • (String, nil) —

    inline value, or value of next token



79
# File 'lib/aspera/cli/command_line.rb', line 79

def value = @inline_value || @value_token&.value