Class: Ark::CLI::Argument

Inherits:
Object
  • Object
show all
Defined in:
lib/ark/cli/argument.rb

Overview

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

Defined Under Namespace

Classes: ArgumentSetError, ArgumentSyntaxError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default = nil, variad: false) ⇒ Argument

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.



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

Instance Attribute Details

#nameObject (readonly)

Return the name of this Argument



75
76
77
# File 'lib/ark/cli/argument.rb', line 75

def name
  @name
end

Class Method Details

.has_default?(arg) ⇒ Boolean

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/ark/cli/argument.rb', line 36

def self.has_default?(arg)
  return !arg[/^\S+?:.+/].nil?
end

.is_glob?(arg) ⇒ Boolean

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

Returns:

  • (Boolean)


46
47
48
# File 'lib/ark/cli/argument.rb', line 46

def self.is_glob?(arg)
  return !arg[/\.\.\.$/].nil?
end

.parse(arg) ⇒ Object

Parse an argument name and return an Argument object



17
18
19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object

Parse the default value from an arg with one



41
42
43
# File 'lib/ark/cli/argument.rb', line 41

def self.parse_default(arg)
  return arg[/^.+?:(.+)/, 1]
end

.strip_arg(arg) ⇒ Object

Strip any special syntax from a given argument name



31
32
33
# File 'lib/ark/cli/argument.rb', line 31

def self.strip_arg(arg)
  return arg.to_s[/^(\S+?)(:|\.\.\.|$)/, 1]
end

.valid_name?(name) ⇒ Boolean

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

Returns:

  • (Boolean)


52
53
54
# File 'lib/ark/cli/argument.rb', line 52

def self.valid_name?(name)
  return !name.to_s[/^[[:alpha:]][[:alnum:]]+$/].nil?
end

Instance Method Details

#fulfilled?Boolean

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 []

Returns:

  • (Boolean)


120
121
122
# File 'lib/ark/cli/argument.rb', line 120

def fulfilled?
  return !@value.nil? || self.has_default?
end

#has_default?Boolean

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/ark/cli/argument.rb', line 113

def has_default?
  return !@default.nil? || self.variadic?
end

#push(val) ⇒ Object

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



90
91
92
93
94
95
# 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) ⇒ Object

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



99
100
101
102
103
104
# 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

#valueObject

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.



80
81
82
83
84
85
86
# File 'lib/ark/cli/argument.rb', line 80

def value
  if @value.nil?
    return @default
  else
    return @value
  end
end

#variadic?Boolean

Return true if this argument is a glob

Returns:

  • (Boolean)


107
108
109
# File 'lib/ark/cli/argument.rb', line 107

def variadic?
  return @variad
end