In Files

Parent

GLI::Flag

Defines a flag, which is to say a switch that takes an argument

Attributes

default_value[R]

Public Class Methods

new(names,description,argument_name=nil,default=nil,long_desc=nil) click to toggle source
# File lib/gli/flag.rb, line 9
def initialize(names,description,argument_name=nil,default=nil,long_desc=nil)
  super(names,description,long_desc)
  @argument_name = argument_name || "arg"
  @default_value = default
end

Public Instance Methods

all_forms(joiner=', ') click to toggle source

Returns a string of all possible forms of this flag. Mostly intended for printing to the user.

# File lib/gli/flag.rb, line 54
def all_forms(joiner=', ')
  forms = all_forms_a
  string = forms.join(joiner)
  if forms[-1] =~ /^\-\-/
    string += '='
  else
    string += ' '
  end
  string += @argument_name
  return string
end
find_me(arg) click to toggle source
# File lib/gli/flag.rb, line 37
def find_me(arg)
  if @names[arg]
    return [true,arg,nil] if arg.length == 2
    # This means we matched the long-form, but there's no argument
    raise "#{arg} requires an argument via #{arg}=argument"
  end
  @names.keys.each() do |name|
    match_string = "^#{name}=(.*)$"
    match_data = arg.match(match_string)
    return [true,name,$1] if match_data;
  end
  [false,nil,nil]
end
get_value!(args) click to toggle source
# File lib/gli/flag.rb, line 15
def get_value!(args)
  args.each_index() do |index|
    arg = args[index]
    present,matched,value = find_me(arg)
    if present
      args.delete_at index
      if !value || value == ''
        if args[index]
          value = args[index]
          args.delete_at index
          return value
        else
          raise "#{matched} requires an argument"
        end
      else
        return value
      end
    end
  end
  return @default_value
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.