class CommandLine::Option
Attributes
alias[R]
default_value[R]
name[R]
parameter_count[R]
Public Class Methods
new(name, definition = {})
click to toggle source
Initialize new CommandLine::Option
name
The name of the flag definition
The definition of the flag.
# File lib/cli/command_line_arguments.rb 16 def initialize(name, definition = {}) 17 @name = CommandLine::Option.rewrite(name) 18 @alias = definition[:alias] ? definition[:alias].to_sym : nil 19 @required = definition.key?(:required) && definition[:required] == true 20 @parameter_count = definition[:parameters] || 1 21 @multiple = definition[:multiple] || false 22 @default_value = definition[:default] || false 23 end
rewrite(sym)
click to toggle source
Rewrites a command line keyword by replacing the underscores with dashes sym
The symbol to rewrite
# File lib/cli/command_line_arguments.rb 9 def self.rewrite(sym) 10 sym.to_s.gsub(/_/, '-').to_sym 11 end
Public Instance Methods
=~(test)
click to toggle source
# File lib/cli/command_line_arguments.rb 49 def =~(test) 50 [@name, @alias].include?(CommandLine::Option.rewrite(test)) 51 end
has_alias?()
click to toggle source
Check if flag has an alias
# File lib/cli/command_line_arguments.rb 64 def has_alias? 65 !@alias.nil? 66 end
has_default?()
click to toggle source
# File lib/cli/command_line_arguments.rb 82 def has_default? 83 !@default_value.nil? 84 end
multiple?()
click to toggle source
# File lib/cli/command_line_arguments.rb 78 def multiple? 79 @multiple 80 end
optional?()
click to toggle source
Check if flag is optional
# File lib/cli/command_line_arguments.rb 74 def optional? 75 !@required 76 end
parse(arguments_parser)
click to toggle source
# File lib/cli/command_line_arguments.rb 25 def parse(arguments_parser) 26 if @parameter_count == 0 27 return true 28 elsif @parameter_count == 1 29 parameter = arguments_parser.next_parameter 30 fail CommandLine::ParameterExpected, self if parameter.nil? 31 return parameter 32 elsif @parameter_count == :any 33 parameters = [] 34 while parameter = arguments_parser.next_parameter && parameter != '--' 35 parameters << parameter 36 end 37 return parameters 38 else 39 parameters = [] 40 @parameter_count.times do |_n| 41 parameter = arguments_parser.next_parameter 42 fail CommandLine::ParameterExpected, self if parameter.nil? 43 parameters << parameter 44 end 45 return parameters 46 end 47 end
required?()
click to toggle source
Check if flag is required
# File lib/cli/command_line_arguments.rb 69 def required? 70 @required 71 end
to_alias()
click to toggle source
Argument alias representation of the flag (-f)
# File lib/cli/command_line_arguments.rb 59 def to_alias 60 "-#{@alias}" 61 end
to_option()
click to toggle source
Argument representation of the flag (–fast)
# File lib/cli/command_line_arguments.rb 54 def to_option 55 "--#{@name}" 56 end