Sha256: 172624d5d3c17f8b430c1fc91af8ddb207725aa2f2cac64d6bca7a9a30d85c51

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

require "mothership/base"
require "mothership/callbacks"
require "mothership/command"
require "mothership/parser"
require "mothership/help"
require "mothership/errors"

class Mothership
  # [Mothership::Command] global options
  @@global = Command.new(self, "(global options)")

  # [Fixnum] exit status; reassign as appropriate error code (e.g. 1)
  @@exit_status = 0

  class << self
    # define a global option
    def option(name, options = {}, &interact)
      @@global.add_input(name, options, &interact)
    end

    # parse argv, by taking the first arg as the command, and the rest as
    # arguments and flags
    #
    # arguments and flags can be in any order; all flags will be parsed out
    # first, and the bits left over will be treated as arguments
    def start(argv)
      global_parser = Parser.new(@@global)
      name, *argv = global_parser.parse_flags(argv, @@commands)

      global = new
      global.input = Inputs.new(@@global, global, global_parser.given)

      return global.default_action unless name

      cmdname = name.gsub("-", "_").to_sym

      cmd = @@commands[cmdname]
      return global.unknown_command(cmdname) unless cmd

      ctx = cmd.context.new
      ctx.input = global.input
      ctx.execute(cmd, argv, global_parser.given)

      code = @@exit_status

      # reset exit status
      @@exit_status = 0

      exit code
    end

    def global_option(name)
      @@global.inputs[name]
    end
  end

  # set the exit status
  def exit_status(num)
    @@exit_status = num
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
mothership-0.3.1 lib/mothership.rb
mothership-0.3.0 lib/mothership.rb
mothership-0.2.5 lib/mothership.rb
mothership-0.2.4 lib/mothership.rb
mothership-0.2.3 lib/mothership.rb
mothership-0.2.2 lib/mothership.rb
mothership-0.2.1 lib/mothership.rb
mothership-0.2.0 lib/mothership.rb