Sha256: e16a6d2a4d9df6caf056f7fd55a6048ff67cecc8c4fb40005689a4f3d1ac8f82

Contents?: true

Size: 1.62 KB

Versions: 23

Compression:

Stored size: 1.62 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)
  end

  def start_command_for(command_name, argv)
    case command_name.to_s

      when 'help'
        show_help_message(argv)

      when 'routes'
        command = Rack::App::CLI::DefaultCommands::ShowRoutes.new
        run_command(argv, command, "routes")

      when 'irb'
        Rack::App::CLI::DefaultCommands::IRB.new.start(argv)

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

    end
  rescue CommandNotFound
    show_commands
  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

23 entries across 23 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/cli/runner.rb
rack-app-11.0.1 lib/rack/app/cli/runner.rb
rack-app-11.0.0 lib/rack/app/cli/runner.rb
rack-app-10.0.0 lib/rack/app/cli/runner.rb
rack-app-9.0.0 lib/rack/app/cli/runner.rb
rack-app-8.1.0 lib/rack/app/cli/runner.rb
rack-app-8.0.1 lib/rack/app/cli/runner.rb
rack-app-8.0.0 lib/rack/app/cli/runner.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/cli/runner.rb
rack-app-7.7.0 lib/rack/app/cli/runner.rb
rack-app-7.6.5 lib/rack/app/cli/runner.rb
rack-app-7.6.4 lib/rack/app/cli/runner.rb
rack-app-7.6.3 lib/rack/app/cli/runner.rb
rack-app-7.6.2 lib/rack/app/cli/runner.rb
rack-app-7.6.1 lib/rack/app/cli/runner.rb
rack-app-7.6.0 lib/rack/app/cli/runner.rb
rack-app-7.5.2 lib/rack/app/cli/runner.rb
rack-app-7.5.1 lib/rack/app/cli/runner.rb
rack-app-7.5.0 lib/rack/app/cli/runner.rb
rack-app-7.4.0 lib/rack/app/cli/runner.rb