Class: CTioga2::Commands::Parsers::CommandLineParser
- Inherits:
-
Object
- Object
- CTioga2::Commands::Parsers::CommandLineParser
- Includes:
- Log
- Defined in:
- lib/ctioga2/commands/parsers/command-line.rb
Overview
This class is in charge of parsing a command-line against a list of known commands.
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
The list of commands.
-
#default_command ⇒ Object
readonly
A [number of args, Command] for the default command, ie the one that applies on non-command files.
-
#long_options ⇒ Object
readonly
A hash ‘long-option-name’ => [number of args, Command].
-
#short_options ⇒ Object
readonly
A hash ‘short-option-letter’ => [number of args, Command].
Instance Method Summary collapse
-
#initialize(commands, default = nil) ⇒ CommandLineParser
constructor
Creates a CommandLineParser that will understand the given commands.
-
#parse_command_line(argv, interpreter) ⇒ Object
Takes an argv array representing the command-line and a target intepreter, and runs the commands found on the command line.
Methods included from Log
context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn
Constructor Details
#initialize(commands, default = nil) ⇒ CommandLineParser
Creates a CommandLineParser that will understand the given commands
57 58 59 60 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 57 def initialize(commands, default = nil) @commands = commands prepare_option_hashes(default) end |
Instance Attribute Details
#commands ⇒ Object (readonly)
The list of commands
49 50 51 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 49 def commands @commands end |
#default_command ⇒ Object (readonly)
A [number of args, Command] for the default command, ie the one that applies on non-command files.
53 54 55 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 53 def default_command @default_command end |
#long_options ⇒ Object (readonly)
A hash ‘long-option-name’ => [number of args, Command]
46 47 48 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 46 def @long_options end |
#short_options ⇒ Object (readonly)
A hash ‘short-option-letter’ => [number of args, Command]
43 44 45 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 43 def @short_options end |
Instance Method Details
#parse_command_line(argv, interpreter) ⇒ Object
Takes an argv array representing the command-line and a target intepreter, and runs the commands found on the command line. Yields arguments which are not part of a command, or feed them to the #default_command if it was specified.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 68 def parse_command_line(argv, interpreter) # We duplicate the original array argv = argv.dup = nil # currently never used. number = 0 while argv.size > 0 current = argv.shift if current =~ /^--(.*)/ # a long option if @long_options.key?($1) command, arguments, = extract_command_arguments(argv, @long_options[$1]) number += 1 interpreter.context.parsing_option(current, number) interpreter.run_command(command, arguments, ) else raise OptionUnkown, "Long option #{current} is not known" end elsif current =~ /^-(.*)/ # Short options # We do the same as above, but splitting into letters first: = $1.split('') for short in if @short_options.key?(short) command, arguments, = extract_command_arguments(argv, @short_options[short]) number += 1 interpreter.context.parsing_option("-#{short}", number) interpreter.run_command(command, arguments, ) else raise OptionUnkown, "Short option -#{short} is not known" end end else if @default_command argv.unshift current command, arguments, = extract_command_arguments(argv, @default_command) number += 1 interpreter.context.parsing_option("(default)", number) interpreter.run_command(command, arguments, ) else yield current end end end end |