Sha256: ea07e1df7e6190e7c9851263e9e16b6445ee6c00f4d7cad129b549f07f9e988b

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

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

class Mothership
  # all commands
  @@commands = {}

  attr_accessor :input

  # Initialize with the command being executed.
  def initialize(command = nil, input = nil)
    @command = command
    @input = input
  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, global = {})
    cmd.invoke({}, Parser.new(cmd).parse_argv(argv), global)
  rescue Mothership::Error => e
    $stderr.puts e
    $stderr.puts ""
    Mothership::Help.command_usage(cmd, $stderr)

    exit_status 1
  end

  # wrap this with your error handling/etc.
  def run(name)
    send(name)
  end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mothership-0.1.5 lib/mothership/base.rb
mothership-0.1.4 lib/mothership/base.rb
mothership-0.1.3 lib/mothership/base.rb
mothership-0.1.2 lib/mothership/base.rb
mothership-0.1.1 lib/mothership/base.rb
mothership-0.1.0 lib/mothership/base.rb