Sha256: c68c53192b36e09455e9687607a88c5c52922ed5d3b9563f3db83861706f5dd4

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 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.actions.each { |a| a.call(argv, @config) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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