Sha256: cd8ca34f06fe15f0d7c3fd6bed88d9c7663c4362f8b9cbafad5594b254f136ce

Contents?: true

Size: 1.42 KB

Versions: 6

Compression:

Stored size: 1.42 KB

Contents

class Cl
  class Parser < OptionParser
    class Format < Struct.new(:opt)
      NAME = /^(--(?:\[no-\])?)([^= ]+)/

      def strs
        strs = opt.strs + aliases
        strs.map { |str| long?(str) ? long(str) : short(str) }.flatten
      end

      def long(str)
        strs = [unnegate(str)]
        strs = strs.map { |str| negated(str) }.flatten if flag?
        strs = collect(strs, :dashed)
        strs = collect(strs, :underscored)
        strs = collect(strs, :valued) if flag?
        strs.uniq
      end

      def short(str)
        str = "#{str} #{opt.name.upcase}" unless opt.flag? || str.include?(' ')
        str
      end

      def unnegate(str)
        str.sub('--[no-]', '--')
      end

      def aliases
        opt.aliases.map { |name| "--#{name} #{ name.upcase unless opt.flag?}".strip }
      end

      def collect(strs, mod)
        strs = strs + strs.map { |str| send(mod, str) }
        strs.flatten.uniq
      end

      def negated(str)
        str.dup.insert(2, '[no-]')
      end

      def dashed(str)
        str =~ NAME && str.sub("#{$1}#{$2}", "#{$1}#{$2.tr('_', '-')}") || str
      end

      def underscored(str)
        str =~ NAME && str.sub("#{$1}#{$2}", "#{$1}#{$2.tr('-', '_')}") || str
      end

      def valued(str)
        "#{str} [true|false|yes|no]"
      end

      def long?(str)
        str.start_with?('--')
      end

      def flag?
        opt.flag? && !opt.help?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cl-1.1.5 lib/cl/parser/format.rb
cl-1.1.4 lib/cl/parser/format.rb
cl-1.1.3 lib/cl/parser/format.rb
cl-1.1.2 lib/cl/parser/format.rb
cl-1.1.1 lib/cl/parser/format.rb
cl-1.1.0 lib/cl/parser/format.rb