Sha256: aa5c49766527f6164b9322b7bcc9b3fc08403a2a1040bc15e388f557ae109a1c

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Mercenary
  class Program < Command
    attr_reader :optparse
    attr_reader :config

    # Public: Creates a new Program
    #
    # name - the name of the program
    #
    # Returns nothing
    def initialize(name)
      @config = {}
      super(name)
    end

    # Public: Sets or gets the program version
    #
    # version - the program version (optional)
    #
    # Returns the version and sets it if an argument is present
    def version(version = nil)
      @version = version if version
      @version
    end

    # Public: Run the program
    #
    # argv   - an array of string args (usually ARGV)
    #
    # Returns nothing
    def go(argv)
      logger.debug("Using args passed in: #{argv.inspect}")

      cmd = nil

      @optparse = OptionParser.new do |opts|
        cmd = super(argv, opts, @config)

        opts.on('-v', '--version', 'Print the version') do
          puts "#{name} #{version}"
          abort
        end
      end

      @optparse.parse!(argv)

      logger.debug("Parsed config: #{@config.inspect}")

      cmd.execute(argv, @config)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mercenary-0.2.1 lib/mercenary/program.rb