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
debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn
Constructor Details
#initialize(commands, default = nil) ⇒ CommandLineParser
Creates a CommandLineParser that will understand the given commands
59 60 61 62 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 59 def initialize(commands, default = nil) @commands = commands prepare_option_hashes(default) end |
Instance Attribute Details
#commands ⇒ Object (readonly)
The list of commands
51 52 53 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 51 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.
55 56 57 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 55 def default_command @default_command end |
#long_options ⇒ Object (readonly)
A hash ‘long-option-name’ => [number of args, Command]
48 49 50 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 48 def @long_options end |
#short_options ⇒ Object (readonly)
A hash ‘short-option-letter’ => [number of args, Command]
45 46 47 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 45 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.
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 |
# File 'lib/ctioga2/commands/parsers/command-line.rb', line 70 def parse_command_line(argv, interpreter) # We duplicate the original array argv = argv.dup = nil # currently never used. 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]) 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]) 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) interpreter.run_command(command, arguments, ) else yield current end end end end |