Class: GitCommander::CLI

Inherits:
Object
  • Object
show all
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.

Examples:

Run a registered “start” command with a single argument

GitCommander::CLI.new.run ["start", "new-feature""]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry: GitCommander::Registry.new, output: STDOUT) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • registry (GitCommander::Registry) (defaults to: GitCommander::Registry.new)

    (GitCommander::Registry.new) the command registry to use for matching available commands

  • output (IO) (defaults to: STDOUT)

    (STDOUT) the IO object you want to use to send output to when running commands



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

#outputObject (readonly)

Returns the value of attribute output.



15
16
17
# File 'lib/git_commander/cli.rb', line 15

def output
  @output
end

#registryObject (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.

Parameters:

  • command (Command)

    the git-cmd command to parse the arguments for

  • arguments (Array)

    the command line arguments

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/git_commander/cli.rb', line 54

def parse_command_options!(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.options
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

Parameters:

  • args (Array) (defaults to: ARGV)

    (ARGV) a list of arguments to pass to the registered 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
  options = parse_command_options!(command, arguments)
  command.run options
rescue Registry::CommandNotFound
  log_command_not_found(command)

  help
rescue StandardError => e
  say e.message
  say e.backtrace
end

#say(message) ⇒ Object



42
43
44
# File 'lib/git_commander/cli.rb', line 42

def say(message)
  output.puts message
end