Module: CommandParser
- Defined in:
- lib/cli/command_parser.rb
Defined Under Namespace
Classes: ParseError
Class Method Summary collapse
-
.parse_options(args) ⇒ Object
Public: Parse options and creates an array of hash data for each.
Class Method Details
.parse_options(args) ⇒ Object
Public: Parse options and creates an array of hash data for each.
args - An Array of options and parameters to parse.
Examples
CommandParser:: "--directory lib/ migrations/ --adapter MySqlPlug"
# => [{ option: 'directory', option_args: ["lib/", "migrations/"] }, { option: 'adapter', option_args: ["MySqlPlug"] }]
Returns a parsed array.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cli/command_parser.rb', line 12 def self.(args) parsed = [] last_option = "" args.each do |chunk| if chunk[/^-*/].empty? raise CommandParser::ParseError.new("Arguments given but no option has been specified.") if last_option.empty? cur_cmd = parsed.last cur_cmd[:options_args] ||= [] cur_cmd[:options_args] << chunk else parsed << { option: chunk.sub(/^-*/, '') } and last_option = chunk end end parsed end |