Class: GitCommander::CLI
- Inherits:
-
Object
- Object
- GitCommander::CLI
- Defined in:
- lib/git_commander/cli.rb
Overview
Manages mapping commands and their arguments that are run from the command-line (via git-cmd) to their corresponding git-commander registered commands.
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
-
#initialize(registry: GitCommander::Registry.new, output: STDOUT) ⇒ CLI
constructor
A new instance of CLI.
-
#parse_command_options!(command, arguments) ⇒ Array<GitCommander::Command::Option>
Parses an array of values (as ARGV would provide) for the provided git-cmd command name.
-
#run(args = ARGV) ⇒ Object
Runs a GitCommander command.
- #say(message) ⇒ Object
Constructor Details
#initialize(registry: GitCommander::Registry.new, output: STDOUT) ⇒ CLI
Returns a new instance of CLI.
20 21 22 23 |
# File 'lib/git_commander/cli.rb', line 20 def initialize(registry: GitCommander::Registry.new, output: STDOUT) @registry = registry @output = output end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
15 16 17 |
# File 'lib/git_commander/cli.rb', line 15 def output @output end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
15 16 17 |
# File 'lib/git_commander/cli.rb', line 15 def registry @registry end |
Instance Method Details
#parse_command_options!(command, arguments) ⇒ Array<GitCommander::Command::Option>
Parses an array of values (as ARGV would provide) for the provided git-cmd command name. The arguments are run through Ruby’s [OptionParser] for validation and then filtered through the command to extract it’s options with any default values.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/git_commander/cli.rb', line 54 def (command, arguments) parser = configure_option_parser_for_command(command) parser.parse!(arguments) # Add arguments to options to pass to defined commands command.arguments.each do |argument| argument.value = arguments.shift end command. rescue OptionParser::InvalidOption, OptionParser::MissingArgument => e command.help GitCommander.logger.debug "[CLI] Failed to parse command line options – #{e.inspect}" exit 1 end |
#run(args = ARGV) ⇒ Object
Runs a GitCommander command
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/git_commander/cli.rb', line 28 def run(args = ARGV) arguments = Array(args) command = registry.find arguments.shift = (command, arguments) command.run rescue Registry::CommandNotFound log_command_not_found(command) help rescue StandardError => e say e. say e.backtrace end |
#say(message) ⇒ Object
42 43 44 |
# File 'lib/git_commander/cli.rb', line 42 def say() output.puts end |