Sha256: 4f6468ef3480742e053491c26e148f213801d51580cc8cb4a898a122177f0d0a

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require 'optparse'

module Tap
  module Support
    
    # Under Construction
    module CommandLine
      module_function

      def usage(path, cols=80)
        parse_usage(File.read(path), cols)
      end
        
      def parse_usage(str, cols=80)
        scanner = StringScanner.new(str)
        scanner.scan(/^#!.*?$/)
        Comment.parse(scanner, false).wrap(cols, 2).strip
      end
      
      def configv(config)
        desc = config.desc
        desc.extend(OptParseComment) if desc.kind_of?(Comment)
          
        [config.short, argtype(config), desc].compact
      end
      
      def argtype(config)
        case config.arg_type
        when :optional 
          "#{config.long} [#{config.arg_name}]"
        when :switch 
          config.long(true)
        when :flag
          config.long
        when :list
          "#{config.long} a,b,c"
        when :mandatory, nil
          "#{config.long} #{config.arg_name}"
        else
          raise "unknown arg_type: #{config.arg_type}"
        end
      end
      
      module OptParseComment
        def empty?
          to_str.empty?
        end

        def to_str
          subject.to_s =~ /#(.*)$/ ? $1.strip : ""
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bahuvrihi-tap-0.10.2 lib/tap/support/command_line.rb
bahuvrihi-tap-0.10.3 lib/tap/support/command_line.rb
bahuvrihi-tap-0.10.4 lib/tap/support/command_line.rb