class GitCommander::CLI

Manages mapping commands and their arguments that are run from the command-line (via `git-cmd`) to their corresponding git-commander registered commands.

@example Run a registered “start” command with a single argument

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

Attributes

output[R]
registry[R]

Public Class Methods

new(registry: GitCommander::Registry.new, output: STDOUT) click to toggle source

@param registry [GitCommander::Registry] (GitCommander::Registry.new) the

command registry to use for matching available commands

@param output [IO] (STDOUT) the IO object you want to use to send output to when running commands

# File lib/git_commander/cli.rb, line 20
def initialize(registry: GitCommander::Registry.new, output: STDOUT)
  @registry = registry
  @output = output
end

Public Instance Methods

parse_command_options!(command, arguments) click to toggle source

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.

@param command [Command] the git-cmd command to parse the arguments for @param arguments [Array] the command line arguments @return [Array<GitCommander::Command::Option>] the available options with values

# 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) click to toggle source

Runs a GitCommander command

@param args [Array] (ARGV) a list of arguments to pass to the registered command

# 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) click to toggle source
# File lib/git_commander/cli.rb, line 42
def say(message)
  output.puts message
end

Private Instance Methods

command_line_flag_formatted_name(flag) click to toggle source
# File lib/git_commander/cli.rb, line 118
def command_line_flag_formatted_name(flag)
  "--#{underscore_to_kebab(flag.name)} #{flag.name.upcase}"
end
configure_flags_for_option_parser_and_command(option_parser, command) click to toggle source
# File lib/git_commander/cli.rb, line 102
def configure_flags_for_option_parser_and_command(option_parser, command)
  command.flags.each do |flag|
    option_parser.on("-#{flag.name[0]}", command_line_flag_formatted_name(flag), flag.description.to_s) do |f|
      flag.value = f
    end
  end
end
configure_option_parser_for_command(command) click to toggle source
# File lib/git_commander/cli.rb, line 89
def configure_option_parser_for_command(command)
  valid_arguments_for_command = command.arguments.map { |arg| "[#{arg.name}]" }.join(" ")

  OptionParser.new do |opts|
    opts.banner = "USAGE:\n    git-cmd #{command.name} [command options] #{valid_arguments_for_command}"
    opts.separator  ""
    opts.separator  "COMMAND OPTIONS:" if command.flags.any? || command.switches.any?

    configure_flags_for_option_parser_and_command(opts, command)
    configure_switches_for_option_parser_and_command(opts, command)
  end
end
configure_switches_for_option_parser_and_command(option_parser, command) click to toggle source
# File lib/git_commander/cli.rb, line 110
def configure_switches_for_option_parser_and_command(option_parser, command)
  command.switches.each do |switch|
    option_parser.on("-#{switch.name[0]}", "--[no-]#{switch.name}", switch.description) do |s|
      switch.value = s
    end
  end
end
help() click to toggle source
# File lib/git_commander/cli.rb, line 72
def help
  say "NAME"
  say "    git-cmd – Git Commander allows running custom git commands from a centralized location."
  say "VERSION"
  say "    #{GitCommander::VERSION}"
  say "USAGE"
  say "    git-cmd command [command options] [arguments...]"
  say "COMMANDS"
  say registry.commands.keys.join(", ")
end
log_command_not_found(command) click to toggle source
# File lib/git_commander/cli.rb, line 83
    def log_command_not_found(command)
      GitCommander.logger.error <<~ERROR_LOG
        #{command} not found in registry.  Available commands: #{registry.commands.keys.inspect}
      ERROR_LOG
    end
underscore_to_kebab(sym_or_string) click to toggle source
# File lib/git_commander/cli.rb, line 122
def underscore_to_kebab(sym_or_string)
  sym_or_string.to_s.gsub("_", "-").to_sym
end