Class: Ark::CLI::Option
- Inherits:
-
Object
- Object
- Ark::CLI::Option
- 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
-
#count ⇒ Object
readonly
A count of how many times this option has been given on the command line.
-
#desc ⇒ Object
readonly
A short description of the option, if given.
-
#long ⇒ Object
readonly
Long name for this option.
-
#short ⇒ Object
readonly
Short name for this option.
Instance Method Summary collapse
-
#arity ⇒ Object
Return the number of arguments this option expects.
-
#flag? ⇒ Boolean
True if this option expects no arguments; opposite of #has_args?.
-
#full? ⇒ Boolean
True if this option has received all the arguments it expects, or if this option expects no arguments.
-
#has_args? ⇒ Boolean
True if this option expects an argument.
-
#header ⇒ Object
Return basic usage information: the option’s names and arguments.
-
#initialize(long, short = nil, args = nil, desc = nil) ⇒ Option
constructor
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. -
#push(val) ⇒ Object
Pass an argument to this option.
-
#to_s ⇒ Object
Represent this option as a string.
-
#toggle ⇒ Object
Toggle this option to the true state and increment the toggle count.
-
#value ⇒ Object
Return the current value of this option.
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
#count ⇒ Object (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 |
#desc ⇒ Object (readonly)
A short description of the option, if given
28 29 30 |
# File 'lib/ark/cli/option.rb', line 28 def desc @desc end |
#long ⇒ Object (readonly)
Long name for this option
31 32 33 |
# File 'lib/ark/cli/option.rb', line 31 def long @long end |
#short ⇒ Object (readonly)
Short name for this option
34 35 36 |
# File 'lib/ark/cli/option.rb', line 34 def short @short end |
Instance Method Details
#arity ⇒ Object
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?
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
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?
87 88 89 |
# File 'lib/ark/cli/option.rb', line 87 def has_args? return @args.length > 0 end |
#header ⇒ Object
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_s ⇒ Object
Represent this option as a string
103 104 105 |
# File 'lib/ark/cli/option.rb', line 103 def to_s() return "(#{self.header})" end |
#toggle ⇒ Object
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 |
#value ⇒ Object
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 |