module GPGStatusParser::Arguments

Constants

ARGUMENTS
DATE
HEX
NO_WHITESPACE

Regexps to extract arguments should have two matchers, the actual argument value and the rest minus whitespace

NUMBER
USERNAME

Public Class Methods

extract_argument_values(expected_args, argument_value_string) click to toggle source
# File lib/gpg_status_parser/arguments.rb, line 137
def self.extract_argument_values expected_args, argument_value_string
  return {} if expected_args.empty?

  results = []
  first, rest = expected_args[0], expected_args[1..-1]

  while first
    extract_re = ARGUMENTS[first]
    match = extract_re.match(argument_value_string)

    if match.nil?
      results << ""
      argument_value_string = ""
      first = nil
      rest = nil
    else
      results << match[1]
      argument_value_string = match[2]
      first, rest = rest[0], rest[1..-1]
    end
    
  end
  
  if argument_value_string && argument_value_string.length > 0
    warn "Actual args didnt match expected.  Extra stuff #{argument_value_string.inspect}"
  end
  
  # Delete unspecified optional args
  args = Hash[expected_args.zip(results)]
  args.delete_if {|key,val| val.nil? || val.empty?}
  args
end
extract_expected_argument(argument_string) click to toggle source
# File lib/gpg_status_parser/arguments.rb, line 99
def self.extract_expected_argument argument_string
  argument_string = argument_string.strip

  return [nil, nil] if argument_string.empty?

  optional = false
  if argument_string[0] == "["
    optional = true
    argument_string = /[ ]*\[(.*)[ ]*\][ ]*$/.match(argument_string)[1]
  end
  

  matches = /<([^>]+)>(.*)/.match(argument_string)

  # make ruby-friendly symbols
  arg = matches[1].gsub(/[ -]/,"_")
  arg = arg.intern

  rest = matches[2]
  
  result = [arg, rest]
  result << :optional if optional

  result
end
extract_expected_arguments(argument_string) click to toggle source
# File lib/gpg_status_parser/arguments.rb, line 125
def self.extract_expected_arguments argument_string
  args = []
  first, rest = extract_expected_argument(argument_string)
  
  while (first || rest)
    args << first
    first, rest = extract_expected_argument(rest)
  end

  args
end