Sha256: af0893f18d57b79e200a0a209ce9efb7020bab335fb092dac0b1f50826da68c6

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

require "optparse"
require "git_commands/command"

module GitCommands
  class CLI
    VALID_COMMANDS = %w[rebase aggregate purge]

    class UnknownCommandError < ArgumentError; end

    def initialize(command_name:, args: ARGV, out: STDOUT, command_klass: Command)
      @command_name = check_command_name(command_name)
      @command_klass = command_klass
      @args = args
      @out = out 
      @repo = nil
      @branches = nil
    end

    def call
      parser.parse!(@args)
      command = @command_klass.new(repo: @repo, branches: @branches)
      command.send(@command_name)
    end

    private def create_command
      return @command if @command
    end

    private def check_command_name(name)
      return name if VALID_COMMANDS.include?(name)
      fail UnknownCommandError, "#{name} is not a supported command"
    end

    private def parser
      OptionParser.new do |opts|
        opts.banner = "Usage: #{@command_name} --repo=./Sites/oro --branches=feature/add_bin,fetaure/remove_rake_task"

        opts.on("-rREPO", "--repo=REPO", "The path to the existing GIT repository") do |repo|
          @repo = repo
        end

        opts.on("-bBRANCHES", "--branches=BRANCHES", "The comma-separated list of branches or the path to a .branches files") do |branches|
          @branches = branches
        end

        opts.on("-h", "--help", "Prints this help") do
          @out.puts opts
          exit
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
git_commands-3.0.5 lib/git_commands/cli.rb
git_commands-3.0.4 lib/git_commands/cli.rb
git_commands-3.0.3 lib/git_commands/cli.rb
git_commands-3.0.2 lib/git_commands/cli.rb
git_commands-3.0.1 lib/git_commands/cli.rb