Sha256: 3f0af5f50c6a28ac0652b3136547940a4d2df613e44dedfefa16f9cb26e7310d

Contents?: true

Size: 1.45 KB

Versions: 2

Compression:

Stored size: 1.45 KB

Contents

require_relative 'option'
require_relative 'exceptions/opts_parser'
require_relative 'options/basic_option'

BBLib.scan_files(File.expand_path('../options', __FILE__), '*.rb') do |file|
  require_relative file
end

module BBLib
  class OptsParser
    include BBLib::Effortless

    attr_ary_of Option, :options, add_rem: true
    attr_str :usage, default: nil, allow_nil: true

    def usage(text = nil)
      @usage = text unless text.nil?
      @usage
    end

    def on(*flags, **opts, &block)
      opts[:type] = :string unless opts[:type]
      add_options(opts.merge(flags: flags, processor: block))
    end

    def parse(args = ARGV)
      parse!(args.dup)
    end

    def parse!(args = ARGV)
      HashStruct.new.tap do |hash|
        options.each do |option|
          option.retrieve(args, hash)
        end
      end.merge(arguments: args)
    end

    def help
      usage.to_s + "\n\t" +
      options.join("\n\t")
    end

    def to_s
      help
    end

    protected

    def method_missing(method, *args, &block)
      if Option.types.include?(method)
        define_singleton_method(method) do |*flags, **opts, &block|
          on(*flags, **opts.merge(type: method), &block)
        end
        send(method, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(method, include_private = false)
      Option.types.include?(method) || super
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bblib-2.0.1 lib/bblib/cli/opts_parser.rb
bblib-2.0.0 lib/bblib/cli/opts_parser.rb