Module: Shellwords

Defined in:
lib/standard/facets/shellwords.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) alt_escape(cmdline)

Escape special characters used in most unix shells to use it, eg. with system().

This differs from Ruby's #escape in that it does not escape shell variables, e.g. $0.



12
13
14
# File 'lib/standard/facets/shellwords.rb', line 12

def alt_escape(cmdline)
  cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s }
end

+ (Object) dos_escape(cmdline)

Escape special character used in DOS-based shells.

TODO: How to integrate with rest of system?

  1. Use platform condition?

  2. Use separate dos_xxx methods?

  3. Put in separate PowerShellwords module?

CREDIT: Lavir the Whiolet



30
31
32
# File 'lib/standard/facets/shellwords.rb', line 30

def dos_escape(cmdline)
  '"' + cmdline.gsub(/\\(?=\\*\")/, "\\\\\\").gsub(/\"/, "\\\"").gsub(/\\$/, "\\\\\\").gsub("%", "%%") + '"'
end

+ (Object) parse(argv, opts) Also known as: run

The coolest little arguments parser in all of Rubyland.

CREDIT: Michel Martens



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/standard/facets/shellwords.rb', line 37

def parse(argv, opts)
  argv = (String === argv ? shellwords(argv) : argv.to_a.dup)
  args = []
  while argv.any?
    item = argv.shift
    flag = opts[item]
    if flag
      # Work around lambda semantics in 1.8.7.
      arity = [flag.arity, 0].max
      # Raise if there are not enough parameters
      # available for the flag.
      if argv.size < arity
        raise ArgumentError
      end
      # Call the lambda with N items from argv,
      # where N is the lambda's arity.
      flag.call(*argv.shift(arity))
    else
      # Collect the items that don't correspond to
      # flags.
      args << item
    end
  end
  args
end

Instance Method Details

- (Object) alt_escape(cmdline) (private)

Escape special characters used in most unix shells to use it, eg. with system().

This differs from Ruby's #escape in that it does not escape shell variables, e.g. $0.



12
13
14
# File 'lib/standard/facets/shellwords.rb', line 12

def alt_escape(cmdline)
  cmdline.gsub(/([\\\t\| &`<>)('"])/) { |s| '\\' << s }
end

- (Object) dos_escape(cmdline) (private)

Escape special character used in DOS-based shells.

TODO: How to integrate with rest of system?

  1. Use platform condition?

  2. Use separate dos_xxx methods?

  3. Put in separate PowerShellwords module?

CREDIT: Lavir the Whiolet



30
31
32
# File 'lib/standard/facets/shellwords.rb', line 30

def dos_escape(cmdline)
  '"' + cmdline.gsub(/\\(?=\\*\")/, "\\\\\\").gsub(/\"/, "\\\"").gsub(/\\$/, "\\\\\\").gsub("%", "%%") + '"'
end

- (Object) escape(cmdline)



17
18
19
# File 'lib/standard/facets/shellwords.rb', line 17

def escape(cmdline)
  cmdline.gsub(/([\\\t\| &`<>)('"])\$/) { |s| '\\' << s }
end

- (Object) parse(argv, opts) (private)

The coolest little arguments parser in all of Rubyland.

CREDIT: Michel Martens



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/standard/facets/shellwords.rb', line 37

def parse(argv, opts)
  argv = (String === argv ? shellwords(argv) : argv.to_a.dup)
  args = []
  while argv.any?
    item = argv.shift
    flag = opts[item]
    if flag
      # Work around lambda semantics in 1.8.7.
      arity = [flag.arity, 0].max
      # Raise if there are not enough parameters
      # available for the flag.
      if argv.size < arity
        raise ArgumentError
      end
      # Call the lambda with N items from argv,
      # where N is the lambda's arity.
      flag.call(*argv.shift(arity))
    else
      # Collect the items that don't correspond to
      # flags.
      args << item
    end
  end
  args
end

- (Object) run (private)

Original name for Shellwords#parse.



64
# File 'lib/standard/facets/shellwords.rb', line 64

alias_method :run, :parse