Sha256: 6090c6f0244743320c448ccb7133b34c8944c07dba0cf9bec334974d772afd4f

Contents?: true

Size: 1.84 KB

Versions: 26

Compression:

Stored size: 1.84 KB

Contents

module Methadone #:nodoc:
  # Assists with parsing strings in the same way that ARGV might.
  # This is *not* used to parse the command-line, but used to 
  # parse config files/environment variables so they can be placed into ARGV and properly interpretted by 
  # OptionParser
  module ARGVParser #:nodoc:

  private

    # Parses +string+, returning an array that can be placed into ARGV or given to OptionParser
    def parse_string_for_argv(string) #:nodoc:
      return [] if string.nil?

      args = []               # return value we are building up
      current = 0             # pointer to where we are in +string+
      next_arg = ''           # the next arg we are building up to ultimatley put into args
      inside_quote = nil      # quote character we are "inside" of
      last_char = nil         # the last character we saw

      while current < string.length
        char = string.chars.to_a[current]
        case char
        when /["']/
          if inside_quote.nil?         # eat the quote, but remember we are now "inside" one
            inside_quote = char
          elsif inside_quote == char   # we closed the quote we were "inside"
            inside_quote = nil
          else                         # we got a different quote, so it goes in literally
            next_arg << char
          end
        when /\s/
          if last_char == "\\"         # we have an escaped space, replace the escape char
            next_arg[-1] = char
          elsif inside_quote           # we are inside a quote so keep the space
            next_arg << char
          else                         # new argument
            args << next_arg
            next_arg = ''
          end
        else
          next_arg << char
        end
        current += 1
        last_char = char
      end
      args << next_arg unless next_arg == ''
      args
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
methadone-2.0.2 lib/methadone/argv_parser.rb
methadone-2.0.1 lib/methadone/argv_parser.rb
methadone-2.0.0 lib/methadone/argv_parser.rb
methadone-1.9.5 lib/methadone/argv_parser.rb
methadone-1.9.4 lib/methadone/argv_parser.rb
methadone-1.9.3 lib/methadone/argv_parser.rb
methadone-1.9.2 lib/methadone/argv_parser.rb
methadone-rehab-1.9.2 lib/methadone/argv_parser.rb
methadone-1.9.1 lib/methadone/argv_parser.rb
methadone-1.9.0 lib/methadone/argv_parser.rb
methadone-1.8.0 lib/methadone/argv_parser.rb
methadone-1.7.0 lib/methadone/argv_parser.rb
methadone-1.6.0 lib/methadone/argv_parser.rb
methadone-1.5.1 lib/methadone/argv_parser.rb
methadone-1.5.0 lib/methadone/argv_parser.rb
methadone-1.4.0 lib/methadone/argv_parser.rb
methadone-1.3.2 lib/methadone/argv_parser.rb
methadone-1.3.1 lib/methadone/argv_parser.rb
methadone-1.3.0 lib/methadone/argv_parser.rb
methadone-1.2.6 lib/methadone/argv_parser.rb