class Toys::Flag::Syntax
Representation of a single flag.
Attributes
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. @return [String]
The style of flag (`:long` or `:short`). @return [:long] if this is a long flag (i.e. double hyphen) @return [:short] if this is a short flag (i.e. single hyphen with one
character).
The type of flag (`:boolean` or `:value`) @return [:boolean] if this is a boolean flag (i.e. no value) @return [:value] if this flag takes a value (even if optional)
The flags (without values) corresponding to this syntax. @return [Array<String>]
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”`. @return [String] The negative form. @return [nil] if the flag has no negative form.
The original string that was parsed to produce this syntax. @return [String]
The flag (without values) corresponding to the normal “positive” form of this flag. @return [String]
A string used to sort this flag compared with others. @return [String]
The original string with the value (if any) stripped, but retaining the `[no-]` prefix if present. @return [String]
The default delimiter used for the value of this flag. This could be `“”` or `“ ”` for a short flag, or `“ ”` or `“=”` for a long flag. @return [String] delimiter @return [nil] if this flag is a boolean flag
The default “label” for the value. e.g. in `–abc=VAL` the label is `“VAL”`. @return [String] the label @return [nil] if this flag is a boolean flag
The type of value (`:required` or `:optional`) @return [:required] if this flag takes a required value @return [:optional] if this flag takes an optional value @return [nil] if this flag is a boolean flag
Public Class Methods
Parse flag syntax @param str [String] syntax.
# File lib/toys/flag.rb, line 456 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
Public Instance Methods
@private
# File lib/toys/flag.rb, line 568 def configure_canonical(canonical_flag_type, canonical_value_type, canonical_value_label, canonical_value_delim) return unless flag_type.nil? @flag_type = canonical_flag_type return unless canonical_flag_type == :value @value_type = canonical_value_type canonical_value_delim = "" if canonical_value_delim == "=" && flag_style == :short canonical_value_delim = "=" if canonical_value_delim == "" && flag_style == :long @value_delim = canonical_value_delim @value_label = canonical_value_label label = @value_type == :optional ? "[#{@value_label}]" : @value_label @canonical_str = "#{str_without_value}#{@value_delim}#{label}" end
Private Instance Methods
# File lib/toys/flag.rb, line 584 def setup(original_str, positive_flag, negative_flag, str_without_value, sort_str, flag_style, flag_type, value_type, value_delim, value_label) @original_str = original_str @positive_flag = positive_flag @negative_flag = negative_flag @flags = [positive_flag] @flags << negative_flag if negative_flag @str_without_value = str_without_value @sort_str = sort_str @flag_style = flag_style @flag_type = flag_type @value_type = value_type @value_delim = value_delim @value_label = value_label ? value_label.upcase : value_label @canonical_str = original_str end