Sha256: 350c0cd3dc80eab2ecddc0a8a7fc0f44b567b6fddf1ee762c295f3898e8a6ee3

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

class Rack::App::CLI::Runner

  CommandNotFound = Class.new(StandardError)

  def initialize(app)
    @cli = app.respond_to?(:cli) ? app.cli : Rack::App::CLI.new
  end

  def start(argv)
    command_name = argv.shift
    start_command_for(command_name,argv)
  end

  protected

  def show_commands
    $stdout.puts(Rack::App::CLI::DefaultCommands::ListCommands.get_message(commands))
  end

  def show_help_message(argv)
    command_name = argv.shift
    command = command_for(command_name)
    options_parser = configure_command(command,command_name)

    $stdout.puts(options_parser.help)
  rescue CommandNotFound
    show_commands
  end

  def start_command_for(command_name, argv)
    case command_name.to_s

      when 'commands'
        return show_commands

      when 'help'
        return show_help_message(argv)

      else
        command = command_for(command_name)
        return run_command(argv, command, command_name)

    end
  end

  def run_command(argv, command, command_name)
    return if command.nil?

    option_parser = configure_command(command, command_name)
    option_parser.parse!(argv)
    command.start(argv)

  end

  def configure_command(command, command_name)
    option_parser = OptionParser.new
    Rack::App::CLI::Command::Configurator.configure(command, command_name, option_parser)
    return option_parser
  end

  def command_for(name)
    commands[(name || raise(CommandNotFound)).to_s.to_sym] || raise(CommandNotFound)
  end

  def commands
    @cli.commands
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-app-3.3.0 lib/rack/app/cli/runner.rb
rack-app-3.2.1 lib/rack/app/cli/runner.rb
rack-app-3.2.0 lib/rack/app/cli/runner.rb