Class: Ark::CLI::Option

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

Overview

Represents an option and stores the option’s current state, as well as usage information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(long, short = nil, args = nil, desc = nil) ⇒ Option

Initialize a new Option instance

keys

A list of names this option will be identified by

args

A list of argument named this option will expect

desc

A short description of this option



12
13
14
15
16
17
18
19
20
# File 'lib/ark/cli/option.rb', line 12

def initialize(long, short=nil, args=nil, desc=nil)
  @long      = long
  @short     = short
  @args      = args || []
  @flag      = false
  @count     = 0
  @desc      = desc || ''
  @arg_index = 0
end

Instance Attribute Details

#countObject (readonly)

A count of how many times this option has been given on the command line. Useful for flags that might be specified repeatedly, like -vvv to raise verbosity three times.



25
26
27
# File 'lib/ark/cli/option.rb', line 25

def count
  @count
end

#descObject (readonly)

A short description of the option, if given



28
29
30
# File 'lib/ark/cli/option.rb', line 28

def desc
  @desc
end

#longObject (readonly)

Long name for this option



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

def long
  @long
end

#shortObject (readonly)

Short name for this option



34
35
36
# File 'lib/ark/cli/option.rb', line 34

def short
  @short
end

Instance Method Details

#arityObject

Return the number of arguments this option expects



37
38
39
# File 'lib/ark/cli/option.rb', line 37

def arity()
  return @args.length
end

#flag?Boolean

True if this option expects no arguments; opposite of #has_args?

Returns:

  • (Boolean)


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

def flag?
  return @args.empty?
end

#full?Boolean

True if this option has received all the arguments it expects, or if this option expects no arguments

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/ark/cli/option.rb', line 43

def full?
  if self.flag?
    return true
  else
    return (@args.length - @arg_index) < 1
  end
end

#has_args?Boolean

True if this option expects an argument. Opposite of #flag?

Returns:

  • (Boolean)


87
88
89
# File 'lib/ark/cli/option.rb', line 87

def has_args?
  return @args.length > 0
end

#headerObject

Return basic usage information: the option’s names and arguments



92
93
94
95
96
97
98
99
100
# File 'lib/ark/cli/option.rb', line 92

def header()
  if self.flag?
    args = ''
  else
    args = ' ' + @args.map {|a| a.name }.join(', ').upcase
  end
  short = @short ? "-#{@short} " : ''
  return "#{short}--#{@long}#{args}"
end

#push(val) ⇒ Object

Pass an argument to this option



69
70
71
72
73
# File 'lib/ark/cli/option.rb', line 69

def push(val)
  arg = @args[@arg_index]
  @arg_index += 1
  arg.set(val)
end

#to_sObject

Represent this option as a string



103
104
105
# File 'lib/ark/cli/option.rb', line 103

def to_s()
  return "(#{self.header})"
end

#toggleObject

Toggle this option to the true state and increment the toggle count. Only valid for options which expect no argument (flags). Attempting to toggle a option with expected arguments will raise an error.



59
60
61
62
63
64
65
66
# File 'lib/ark/cli/option.rb', line 59

def toggle()
  if self.flag?
    @count += 1
    @flag = true
  else
    raise StandardError, "Tried to toggle an option which expects an argument"
  end
end

#valueObject

Return the current value of this option



76
77
78
79
80
81
82
83
84
# File 'lib/ark/cli/option.rb', line 76

def value()
  if self.flag?
    return @flag
  else
    vals = @args.map {|a| a.value }
    vals = vals.first if vals.length == 1
    return vals
  end
end