@@ -28,38 +28,34 @@ def escape_cmd(executable)
28
28
# to cmd.exe, whereas this is expected to be passed directly to the Win32 API, anticipating that it
29
29
# will in turn be interpreted by CommandLineToArgvW.
30
30
def argv_to_commandline ( args )
31
- escaped_args = args . map do |arg |
32
- escape_arg ( arg )
33
- end
31
+ escaped_args = args . map { |arg | escape_arg ( arg ) }
34
32
35
33
escaped_args . join ( ' ' )
36
34
end
37
35
38
36
# Escape an individual argument per Windows shell rules
39
37
# @param arg [String] Shell argument
40
38
def escape_arg ( arg )
41
- needs_quoting = space_chars . any? do |char |
42
- arg . include? ( char )
43
- end
39
+ needs_quoting = space_chars . any? { |char | arg . include? ( char ) }
44
40
45
- # Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote
46
- # We need to send double the number of backslashes to make it work as expected
47
- # See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks
48
- arg = arg . gsub ( /(\\ *)"/ , '\\1\\1"' )
41
+ # Fix the weird behaviour when backslashes are treated differently when immediately prior to a double-quote
42
+ # We need to send double the number of backslashes to make it work as expected
43
+ # See: https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-commandlinetoargvw#remarks
44
+ arg = arg . gsub ( /(\\ *)"/ , '\\1\\1"' )
49
45
50
- # Quotes need to be escaped
51
- arg = arg . gsub ( '"' , '\\"' )
46
+ # Quotes need to be escaped
47
+ arg = arg . gsub ( '"' , '\\"' )
52
48
53
- if needs_quoting
54
- # At the end of the argument, we're about to add another quote - so any backslashes need to be doubled here too
55
- arg = arg . gsub ( /(\\ *)$/ , '\\1\\1' )
56
- arg = "\" #{ arg } \" "
57
- end
49
+ if needs_quoting
50
+ # At the end of the argument, we're about to add another quote - so any backslashes need to be doubled here too
51
+ arg = arg . gsub ( /(\\ *)$/ , '\\1\\1' )
52
+ arg = "\" #{ arg } \" "
53
+ end
58
54
59
- # Empty string needs to be coerced to have a value
60
- arg = '""' if arg == ''
55
+ # Empty string needs to be coerced to have a value
56
+ arg = '""' if arg == ''
61
57
62
- arg
58
+ arg
63
59
end
64
60
65
61
# Convert the executable and argument array to a command that can be run in this command shell
0 commit comments