Sha256: 872dbf9c32af2be6bd7792a5259c9477f4e2f7d78d6829a474df9cd375009ed2

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 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
    puts e
    puts ""
    Mothership::Help.command_usage(cmd)

    @@exit_status = 1
  end

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mothership-0.0.7 lib/mothership/base.rb
mothership-0.0.6 lib/mothership/base.rb
mothership-0.0.5 lib/mothership/base.rb
mothership-0.0.4 lib/mothership/base.rb
mothership-0.0.3 lib/mothership/base.rb
mothership-0.0.2 lib/mothership/base.rb
mothership-0.0.1 lib/mothership/base.rb