Sha256: 7b275c927d06b4d3cf2b6684ce673bdaec42d1c7510ef3f8ac035ed6ef5c5ef9

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require 'gli/command_line_token.rb'

module GLI
  # Defines a command line switch
  class Switch < CommandLineToken

    def initialize(names,description)
      super(names,description)
    end

    # Given the argument list, scans it looking for this switch
    # returning true if it's in the argumennt list (and removing it from the argument list)
    def get_value!(args)
      idx = -1
      args.each_index do |i|
        result = find_me(args[i])
        if result[0]
          if result[1]
            args[i] = result[1]
          else
            args.delete_at i
          end
          return result[0]
        end
      end
      false
    end

    # Finds the switch in the given arg, returning the arg to keep.
    # Returns an array of size 2:
    # [0] true or false if the arg was found
    # [1] the remaining arg to keep in the command line or nil to remove it
    def find_me(arg)
      if @names[arg]
        return [true,nil]
      end
      @names.keys.each() do |name|
        if name =~ /^-(\w)$/
          match_string = "^\\-(\\w*)#{$1}(\\w*)$"
          match_data = arg.match(match_string)
          if match_data
            # Note that if [1] and [2] were both empty 
            # we'd have returned above
            return [true, "-" + match_data[1] + match_data[2]]
          end
        end
      end
      [false]
    end

    def self.name_as_string(name)
      string = name.to_s
      string.length == 1 ? "-#{string}" : "--#{string}"
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
davetron5000-gli-0.1.4 lib/gli/switch.rb
davetron5000-gli-0.1.5 lib/gli/switch.rb
davetron5000-gli-0.1.6 lib/gli/switch.rb
davetron5000-gli-0.2.0 lib/gli/switch.rb
gli-0.1.6 lib/gli/switch.rb