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