Class: RVGP::Base::Command::Option
- Inherits:
-
Object
- Object
- RVGP::Base::Command::Option
- Defined in:
- lib/rvgp/base/command.rb
Overview
Option(s) are, as the name would imply, a command line option, that is prefixed with one or more dashes. Whereas some arguments are program targets, options typically expresses a global program setting, to take effect during this execution.
Some options are binaries, and are presumed ‘off’ if unspecified. Other options are key/value pairs, separated by an equal sign or space, in a form such as “-d ~/ledger” or “–dir=~/ledger”. Option keys are expected to exist in both a short and long form. In the previous example, both the “-d” and “–dir” examples are identical. The “-d” form is a short form and “–dir” is a long form, of the same Option.
This class offers common functions for specifying and parsing options on the command line, as well as for producing the documentation on an option.
Defined Under Namespace
Classes: UnexpectedEndOfArgs
Instance Attribute Summary collapse
-
#long ⇒ Symbol
readonly
A multi-character code, which identifies this option.
-
#short ⇒ Symbol
readonly
A one character code, which identifies this option.
Class Method Summary collapse
-
.remove_options_from_args(options, args) ⇒ Array<Hash<Symbol,Object>,Array<String>>
Given program arguments, and an array of options that we wish to support, return the options and arguments that were encountered.
Instance Method Summary collapse
-
#initialize(long, short, options = {}) ⇒ Option
constructor
Create a new Option.
-
#matches?(str) ⇒ TrueClass, FalseClass
Returns true, if either our short or long form, equals the provided string.
-
#value? ⇒ TrueClass, FalseClass
Returns true, if we expect our key to be paired with a value.
Constructor Details
#initialize(long, short, options = {}) ⇒ Option
Create a new Option
193 194 195 196 197 |
# File 'lib/rvgp/base/command.rb', line 193 def initialize(long, short, = {}) @short = short.to_sym @long = long.to_sym @has_value = [:has_value] if .key? :has_value end |
Instance Attribute Details
#long ⇒ Symbol (readonly)
A multi-character code, which identifies this option
180 181 182 |
# File 'lib/rvgp/base/command.rb', line 180 def long @long end |
#short ⇒ Symbol (readonly)
A one character code, which identifies this option
180 181 182 |
# File 'lib/rvgp/base/command.rb', line 180 def short @short end |
Class Method Details
.remove_options_from_args(options, args) ⇒ Array<Hash<Symbol,Object>,Array<String>>
Given program arguments, and an array of options that we wish to support, return the options and arguments that were encountered.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/rvgp/base/command.rb', line 225 def self.(, args) ret_args = [] = {} i = 0 until i >= args.length arg = args[i] arg_value = nil if /\A([^=]+)=([^ ]+)/.match arg arg = ::Regexp.last_match 1 arg_value = ::Regexp.last_match 2 end option = .find { |opt| opt.matches? arg } if option [option.long] = if option.value? if arg_value.nil? if i + 1 >= args.length raise UnexpectedEndOfArgs, I18n.t('error.end_of_args') end i += 1 args[i] else arg_value end else true end else ret_args << args[i] end i += 1 end [, ret_args] end |
Instance Method Details
#matches?(str) ⇒ TrueClass, FalseClass
Returns true, if either our short or long form, equals the provided string
202 203 204 |
# File 'lib/rvgp/base/command.rb', line 202 def matches?(str) ["--#{long}", "-#{short}"].include? str end |
#value? ⇒ TrueClass, FalseClass
Returns true, if we expect our key to be paired with a value. This property is specified in the :has_value option in the constructor.
209 210 211 |
# File 'lib/rvgp/base/command.rb', line 209 def value? !@has_value.nil? end |