class Ark::CLI::Argument

Represents an argument, either to the program itself or for options which take arguments.

Attributes

name[R]

Return the name of this Argument

Public Class Methods

has_default?(arg) click to toggle source

Return true if the given argument has a default value, like +‘arg:defaultvalue’+

# File lib/ark/cli/argument.rb, line 36
def self.has_default?(arg)
  return !arg[/^\S+?:.+/].nil?
end
is_glob?(arg) click to toggle source

Return true if the given argument is a glob, like +‘arg…’+

# File lib/ark/cli/argument.rb, line 46
def self.is_glob?(arg)
  return !arg[/\.\.\.$/].nil?
end
new(name, default=nil, variad: false) click to toggle source

Initialize a new Argument object. name must be alphanumeric and must begin with a letter. If this argument is unfulfilled, default will be returned as its value, if default is non-nil. If variad is true, then this argument will act as a glob for all trailing args.

# File lib/ark/cli/argument.rb, line 60
def initialize(name, default=nil, variad: false)
  unless self.class.valid_name?(name)
    raise ArgumentSyntaxError, "Invalid argument name: #{name}"
  end
  @name = name.to_s
  @default = default
  @variad = variad
  if self.variadic?
    @value = []
  else
    @value = nil
  end
end
parse(arg) click to toggle source

Parse an argument name and return an Argument object

# File lib/ark/cli/argument.rb, line 17
def self.parse(arg)
  arg = arg.to_s
  name = self.strip_arg(arg)
  if self.has_default?(arg)
    default = self.parse_default(arg)
    return Argument.new(name, default)
  elsif self.is_glob?(arg)
    return Argument.new(name, variad: true)
  else
    return Argument.new(name)
  end
end
parse_default(arg) click to toggle source

Parse the default value from an arg with one

# File lib/ark/cli/argument.rb, line 41
def self.parse_default(arg)
  return arg[/^.+?:(.+)/, 1]
end
strip_arg(arg) click to toggle source

Strip any special syntax from a given argument name

# File lib/ark/cli/argument.rb, line 31
def self.strip_arg(arg)
  return arg.to_s[/^(\S+?)(:|\.\.\.|$)/, 1]
end
valid_name?(name) click to toggle source

Validate an option name. Names must be alphanumeric, and must begin with a letter.

# File lib/ark/cli/argument.rb, line 52
def self.valid_name?(name)
  return !name.to_s[/^[[:alpha:]][[:alnum:]]+$/].nil?
end

Public Instance Methods

fulfilled?() click to toggle source

Return true if this argument has been given a value, or if it has a default value. Variadic arguments will always return true, since they are never required and always have a default value of []

# File lib/ark/cli/argument.rb, line 120
def fulfilled?
  return !@value.nil? || self.has_default?
end
has_default?() click to toggle source

Return true if this argument has a default value. Variadic arguments always return true

# File lib/ark/cli/argument.rb, line 113
def has_default?
  return !@default.nil? || self.variadic?
end
push(val) click to toggle source

Push val onto this argument. Only valid for variadic args. For normal arguments, use set instead.

# File lib/ark/cli/argument.rb, line 90
def push(val)
  unless self.variadic?
    raise ArgumentSetError, "Cannot push onto a normal argument. Use the #set method instead."
  end
  @value << val
end
set(val) click to toggle source

Set the value for this argument to val. Only valid for non-variadic arguments. For variadic args, use push instead.

# File lib/ark/cli/argument.rb, line 99
def set(val)
  if self.variadic?
    raise ArgumentSetError, "Cannot set the value of a glob, use the #push method instead."
  end
  @value = val
end
value() click to toggle source

Return the value for this argument. The default value will be returned if the argument is unset and the default is non-nil. If the argument is unset and there is no default, return nil.

# File lib/ark/cli/argument.rb, line 80
def value
  if @value.nil?
    return @default
  else
    return @value
  end
end
variadic?() click to toggle source

Return true if this argument is a glob

# File lib/ark/cli/argument.rb, line 107
def variadic?
  return @variad
end