Sha256: 549aefacbcf29387d79a3ca5c88cd9fec85564f1b094ae9e3a08114b01bfdde6

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require "mothership/command"
require "mothership/inputs"

class Mothership
  # all commands
  @@commands = {}

  # parsed global input set
  @@inputs = nil

  # Initialize with the command being executed.
  def initialize(command = nil)
    @command = command
  end

  class << self
    # start defining a new command with the given description
    def desc(description)
      @command = Command.new(self, description)
    end

    # define an input for the current command or the global command
    def input(name, options = {}, &default)
      raise "no current command" unless @command

      @command.add_input(name, options, &default)
    end

    # register a command
    def method_added(name)
      return unless @command

      @command.name = name
      @@commands[name] = @command

      @command = nil
    end

    def alias_command(orig, new)
      @@commands[new] = @@commands[orig]
    end
  end

  def execute(cmd, argv)
    cmd.invoke(Parser.new(cmd).inputs(argv))
  rescue Mothership::Error => e
    $stderr.puts e
    $stderr.puts ""
    Mothership::Help.command_usage(cmd, $stderr)

    exit_status 1
  end

  # invoke a command with inputs
  def invoke(name, inputs = {}, given = {})
    if cmd = @@commands[name]
      cmd.invoke(given, inputs)
    else
      unknown_command(name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mothership-0.0.15 lib/mothership/base.rb
mothership-0.0.14 lib/mothership/base.rb