4
4
5
5
const shell_special = " #{}()[]<>|&*?~;"
6
6
7
- function shell_parse (str:: AbstractString , interpolate:: Bool = true )
7
+ # needs to be factored out so depwarn only warns once
8
+ @noinline warn_shell_special (special) =
9
+ depwarn (" special characters \" $special \" should now be quoted in commands" , :warn_shell_special )
10
+
11
+ function shell_parse (
12
+ str:: AbstractString ,
13
+ interpolate:: Bool = true ;
14
+ special:: AbstractString = " "
15
+ )
8
16
s = lstrip (str)
9
17
# strips the end but respects the space when the string ends with "\\ "
10
18
r = RevString (s)
@@ -94,8 +102,8 @@ function shell_parse(str::AbstractString, interpolate::Bool=true)
94
102
update_arg (s[i: j- 1 ]); i = k
95
103
c, k = next (s,k)
96
104
end
97
- elseif ! in_single_quotes && ! in_double_quotes && c in shell_special
98
- depwarn ( " special characters \" $shell_special \" should now be quoted in commands " , :shell_parse )
105
+ elseif ! in_single_quotes && ! in_double_quotes && c in special
106
+ warn_shell_special ( special) # noinline depwarn
99
107
end
100
108
j = k
101
109
end
@@ -118,15 +126,15 @@ function shell_parse(str::AbstractString, interpolate::Bool=true)
118
126
end
119
127
120
128
function shell_split (s:: AbstractString )
121
- parsed = shell_parse (s,false )[1 ]
129
+ parsed = shell_parse (s, false )[1 ]
122
130
args = AbstractString[]
123
131
for arg in parsed
124
132
push! (args, string (arg... ))
125
133
end
126
134
args
127
135
end
128
136
129
- function print_shell_word (io:: IO , word:: AbstractString , special:: AbstractString = shell_special )
137
+ function print_shell_word (io:: IO , word:: AbstractString , special:: AbstractString = " " )
130
138
if isempty (word)
131
139
print (io, " ''" )
132
140
end
@@ -158,30 +166,30 @@ end
158
166
159
167
function print_shell_escaped (
160
168
io:: IO , cmd:: AbstractString , args:: AbstractString... ;
161
- special:: AbstractString = shell_special
169
+ special:: AbstractString = " "
162
170
)
163
171
print_shell_word (io, cmd, special)
164
172
for arg in args
165
173
print (io, ' ' )
166
174
print_shell_word (io, arg, special)
167
175
end
168
176
end
169
- print_shell_escaped (io:: IO ; special:: String = shell_special ) = nothing
177
+ print_shell_escaped (io:: IO ; special:: String = " " ) = nothing
170
178
171
179
"""
172
- shell_escape(args::Union{Cmd,AbstractString...}; special::AbstractString="$shell_special ")
180
+ shell_escape(args::Union{Cmd,AbstractString...}; special::AbstractString="")
173
181
174
182
The unexported `shell_escape` function is the inverse of the unexported `shell_split` function:
175
183
it takes a string or command object and escapes any special characters in such a way that calling
176
184
`shell_split` on it would give back the array of words in the original command. The `special`
177
185
keyword argument controls what characters in addition to whitespace, backslashes, quotes and
178
- dollar signs are considered to be special. Examples:
186
+ dollar signs are considered to be special (default: none). Examples:
187
+
188
+ julia> Base.shell_escape("cat", "/foo/bar baz", "&&", "echo", "done")
189
+ "cat '/foo/bar baz' && echo done"
179
190
180
191
julia> Base.shell_escape("echo", "this", "&&", "that")
181
192
"echo this '&&' that"
182
-
183
- julia> Base.shell_escape("cat", "/foo/bar baz", "&&", "echo", "done", special="")
184
- "cat '/foo/bar baz' && echo done"
185
193
"""
186
- shell_escape (args:: AbstractString... ; special:: AbstractString = shell_special ) =
194
+ shell_escape (args:: AbstractString... ; special:: AbstractString = " " ) =
187
195
sprint (io-> print_shell_escaped (io, args... , special= special))
0 commit comments