Class: Toys::Flag::Syntax

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/flag.rb

Overview

Representation of a single flag.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ Syntax

Parse flag syntax

Parameters:

  • str (String)

    syntax.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/toys/flag.rb', line 19

def initialize(str)
  case str
  when /\A(-([?\w]))\z/
    setup(str, $1, nil, $1, $2, :short, nil, nil, nil, nil)
  when /\A(-([?\w]))(?:( ?)\[|\[( ))(\w+)\]\z/
    setup(str, $1, nil, $1, $2, :short, :value, :optional, $3 || $4, $5)
  when /\A(-([?\w]))( ?)(\w+)\z/
    setup(str, $1, nil, $1, $2, :short, :value, :required, $3, $4)
  when /\A--\[no-\](\w[?\w-]*)\z/
    setup(str, "--#{$1}", "--no-#{$1}", str, $1, :long, :boolean, nil, nil, nil)
  when /\A(--(\w[?\w-]*))\z/
    setup(str, $1, nil, $1, $2, :long, nil, nil, nil, nil)
  when /\A(--(\w[?\w-]*))(?:([= ])\[|\[([= ]))(\w+)\]\z/
    setup(str, $1, nil, $1, $2, :long, :value, :optional, $3 || $4, $5)
  when /\A(--(\w[?\w-]*))([= ])(\w+)\z/
    setup(str, $1, nil, $1, $2, :long, :value, :required, $3, $4)
  else
    raise ToolDefinitionError, "Illegal flag: #{str.inspect}"
  end
end

Instance Attribute Details

#canonical_strString (readonly)

A canonical string representing this flag's syntax, normalized to match the type, delimiters, etc. settings of other flag syntaxes. This is generally used in help strings to represent this flag.

Returns:

  • (String)


128
129
130
# File 'lib/toys/flag.rb', line 128

def canonical_str
  @canonical_str
end

#flag_style:long, :short (readonly)

The style of flag (:long or :short).

Returns:

  • (:long)

    if this is a long flag (i.e. double hyphen)

  • (:short)

    if this is a short flag (i.e. single hyphen with one character).



89
90
91
# File 'lib/toys/flag.rb', line 89

def flag_style
  @flag_style
end

#flag_type:boolean, :value (readonly)

The type of flag (:boolean or :value)

Returns:

  • (:boolean)

    if this is a boolean flag (i.e. no value)

  • (:value)

    if this flag takes a value (even if optional)



96
97
98
# File 'lib/toys/flag.rb', line 96

def flag_type
  @flag_type
end

#flagsArray<String> (readonly)

The flags (without values) corresponding to this syntax.

Returns:

  • (Array<String>)


52
53
54
# File 'lib/toys/flag.rb', line 52

def flags
  @flags
end

#negative_flagString? (readonly)

The flag (without values) corresponding to the "negative" form of this flag, if any. i.e. if the original string was "--[no-]abc", the negative flag is "--no-abc".

Returns:

  • (String)

    The negative form.

  • (nil)

    if the flag has no negative form.



68
69
70
# File 'lib/toys/flag.rb', line 68

def negative_flag
  @negative_flag
end

#original_strString (readonly)

The original string that was parsed to produce this syntax.

Returns:

  • (String)


46
47
48
# File 'lib/toys/flag.rb', line 46

def original_str
  @original_str
end

#positive_flagString (readonly)

The flag (without values) corresponding to the normal "positive" form of this flag.

Returns:

  • (String)


59
60
61
# File 'lib/toys/flag.rb', line 59

def positive_flag
  @positive_flag
end

#sort_strString (readonly)

A string used to sort this flag compared with others.

Returns:

  • (String)


81
82
83
# File 'lib/toys/flag.rb', line 81

def sort_str
  @sort_str
end

#str_without_valueString (readonly)

The original string with the value (if any) stripped, but retaining the [no-] prefix if present.

Returns:

  • (String)


75
76
77
# File 'lib/toys/flag.rb', line 75

def str_without_value
  @str_without_value
end

#value_delimString? (readonly)

The default delimiter used for the value of this flag. This could be "" or " " for a short flag, or " " or "=" for a long flag.

Returns:

  • (String)

    delimiter

  • (nil)

    if this flag is a boolean flag



112
113
114
# File 'lib/toys/flag.rb', line 112

def value_delim
  @value_delim
end

#value_labelString? (readonly)

The default "label" for the value. e.g. in --abc=VAL the label is "VAL".

Returns:

  • (String)

    the label

  • (nil)

    if this flag is a boolean flag



120
121
122
# File 'lib/toys/flag.rb', line 120

def value_label
  @value_label
end

#value_type:required, ... (readonly)

The type of value (:required or :optional)

Returns:

  • (:required)

    if this flag takes a required value

  • (:optional)

    if this flag takes an optional value

  • (nil)

    if this flag is a boolean flag



104
105
106
# File 'lib/toys/flag.rb', line 104

def value_type
  @value_type
end