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, defaults = 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(arg) ⇒ Object
Pass an argument
arg
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.
-
#vals_needed ⇒ Object
Return a count of how many arguments this option still expects.
-
#value ⇒ Object
Return the current value of this option.
Constructor Details
#initialize(long, short = nil, args = nil, defaults = 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 21 |
# File 'lib/ark/cli/option.rb', line 12 def initialize(long, short=nil, args=nil, defaults=nil, desc=nil) @long = long @short = short @args = args || [] @defaults = defaults || {} @vals = [] @flag = false @count = 0 @desc = desc || '' 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.
26 27 28 |
# File 'lib/ark/cli/option.rb', line 26 def count @count end |
#desc ⇒ Object (readonly)
A short description of the option, if given
29 30 31 |
# File 'lib/ark/cli/option.rb', line 29 def desc @desc end |
#long ⇒ Object (readonly)
Long name for this option
32 33 34 |
# File 'lib/ark/cli/option.rb', line 32 def long @long end |
#short ⇒ Object (readonly)
Short name for this option
35 36 37 |
# File 'lib/ark/cli/option.rb', line 35 def short @short end |
Instance Method Details
#arity ⇒ Object
Return the number of arguments this option expects
38 39 40 |
# File 'lib/ark/cli/option.rb', line 38 def arity() return @args.length end |
#flag? ⇒ Boolean
True if this option expects no arguments; opposite of #has_args?
58 59 60 |
# File 'lib/ark/cli/option.rb', line 58 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
53 54 55 |
# File 'lib/ark/cli/option.rb', line 53 def full? return self.vals_needed == 0 end |
#has_args? ⇒ Boolean
True if this option expects an argument. Opposite of #flag?
99 100 101 |
# File 'lib/ark/cli/option.rb', line 99 def has_args? return @args.length > 0 end |
#header ⇒ Object
Return basic usage information: the option’s names and arguments
104 105 106 107 108 109 110 111 112 |
# File 'lib/ark/cli/option.rb', line 104 def header() if self.flag? args = '' else args = ' ' + @args.join(', ').upcase end short = @short ? "-#{@short} " : '' return "#{short}--#{@long}#{args}" end |
#push(arg) ⇒ Object
Pass an argument arg
to this option
75 76 77 |
# File 'lib/ark/cli/option.rb', line 75 def push(arg) @vals << arg end |
#to_s ⇒ Object
Represent this option as a string
115 116 117 |
# File 'lib/ark/cli/option.rb', line 115 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.
65 66 67 68 69 70 71 72 |
# File 'lib/ark/cli/option.rb', line 65 def toggle() if self.flag? @count += 1 @flag = true else raise StandardError, "Tried to toggle an option which expects an argument" end end |
#vals_needed ⇒ Object
Return a count of how many arguments this option still expects
43 44 45 46 47 48 49 |
# File 'lib/ark/cli/option.rb', line 43 def vals_needed() if self.flag? return 0 else return @args.length - @vals.length end end |
#value ⇒ Object
Return the current value of this option
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ark/cli/option.rb', line 80 def value() if self.flag? return @flag else if self.full? && @vals.length == 1 return @vals[0] elsif self.full? return @vals else if !@defaults.compact.empty? return @defaults.length > 1 ? @defaults : @defaults.first else return nil end end end end |