Class: Lucie::Core::CommandLineParser
- Inherits:
-
Object
- Object
- Lucie::Core::CommandLineParser
- Defined in:
- lib/lucie/command_line_parser.rb
Overview
Command line parser extracts the controller name and action from the application input and prepares the params hash for the controller.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Gets the value of an option.
-
#args ⇒ Object
Get the values which has no options.
-
#has_arg?(option) ⇒ Boolean
Checks whether the value is in the args.
-
#has_option?(option) ⇒ Boolean
Is the option defined?.
-
#initialize(parameters) ⇒ CommandLineParser
constructor
Initializes a CommandLineParser.
-
#pair(short, long) ⇒ Object
Pairs short and long options.
-
#shift ⇒ Object
Shift the parameter list.
Constructor Details
#initialize(parameters) ⇒ CommandLineParser
Initializes a CommandLineParser
18 19 20 21 22 23 24 25 |
# File 'lib/lucie/command_line_parser.rb', line 18 def initialize(parameters) @params_str = parameters.class == Array ? parameters.join(" ") : parameters @options = {} @options[:args] = [] @latest_option = nil () end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/lucie/command_line_parser.rb', line 12 def @options end |
Instance Method Details
#[](name) ⇒ Object
Gets the value of an option
For “–option value” the self returns “value”
32 33 34 |
# File 'lib/lucie/command_line_parser.rb', line 32 def [](name) @options[name] end |
#args ⇒ Object
Get the values which has no options.
Arguments are values without any option or tag. When the option name is undefined, the value gets placed in args.
74 75 76 |
# File 'lib/lucie/command_line_parser.rb', line 74 def args @options[:args].dup end |
#has_arg?(option) ⇒ Boolean
Checks whether the value is in the args.
82 83 84 |
# File 'lib/lucie/command_line_parser.rb', line 82 def has_arg?(option) @options[:args].include?(option) end |
#has_option?(option) ⇒ Boolean
Is the option defined?
65 66 67 |
# File 'lib/lucie/command_line_parser.rb', line 65 def has_option?(option) @options[remove_dashes(option).to_sym] || false end |
#pair(short, long) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/lucie/command_line_parser.rb', line 51 def pair(short, long) short_p = remove_dashes(short).to_sym long_p = remove_dashes(long).to_sym if @options[short_p].class == String @options[long_p] = @options[short_p] else @options[short_p] = @options[long_p] end end |
#shift ⇒ Object
Shift the parameter list. Useful for removing the controller’s name from the beginning.
39 40 41 |
# File 'lib/lucie/command_line_parser.rb', line 39 def shift @options[:args].shift end |