-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shell_(parse|split|escape): fix some issues with deprecation #19985
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,15 @@ | |
|
||
const shell_special = "#{}()[]<>|&*?~;" | ||
|
||
function shell_parse(str::AbstractString, interpolate::Bool=true) | ||
# needs to be factored out so depwarn only warns once | ||
@noinline warn_shell_special(special) = | ||
depwarn("special characters \"$special\" should now be quoted in commands", :warn_shell_special) | ||
|
||
function shell_parse( | ||
str::AbstractString, | ||
interpolate::Bool=true; | ||
special::AbstractString="" | ||
) | ||
s = lstrip(str) | ||
# strips the end but respects the space when the string ends with "\\ " | ||
r = RevString(s) | ||
|
@@ -94,8 +102,8 @@ function shell_parse(str::AbstractString, interpolate::Bool=true) | |
update_arg(s[i:j-1]); i = k | ||
c, k = next(s,k) | ||
end | ||
elseif !in_single_quotes && !in_double_quotes && c in shell_special | ||
depwarn("special characters \"$shell_special\" should now be quoted in commands", :shell_parse) | ||
elseif !in_single_quotes && !in_double_quotes && c in special | ||
warn_shell_special(special) # noinline depwarn | ||
end | ||
j = k | ||
end | ||
|
@@ -118,15 +126,15 @@ function shell_parse(str::AbstractString, interpolate::Bool=true) | |
end | ||
|
||
function shell_split(s::AbstractString) | ||
parsed = shell_parse(s,false)[1] | ||
parsed = shell_parse(s, false)[1] | ||
args = AbstractString[] | ||
for arg in parsed | ||
push!(args, string(arg...)) | ||
end | ||
args | ||
end | ||
|
||
function print_shell_word(io::IO, word::AbstractString, special::AbstractString = shell_special) | ||
function print_shell_word(io::IO, word::AbstractString, special::AbstractString = "") | ||
if isempty(word) | ||
print(io, "''") | ||
end | ||
|
@@ -158,30 +166,30 @@ end | |
|
||
function print_shell_escaped( | ||
io::IO, cmd::AbstractString, args::AbstractString...; | ||
special::AbstractString=shell_special | ||
special::AbstractString="" | ||
) | ||
print_shell_word(io, cmd, special) | ||
for arg in args | ||
print(io, ' ') | ||
print_shell_word(io, arg, special) | ||
end | ||
end | ||
print_shell_escaped(io::IO; special::String=shell_special) = nothing | ||
print_shell_escaped(io::IO; special::String="") = nothing | ||
|
||
""" | ||
shell_escape(args::Union{Cmd,AbstractString...}; special::AbstractString="$shell_special") | ||
shell_escape(args::Union{Cmd,AbstractString...}; special::AbstractString="") | ||
|
||
The unexported `shell_escape` function is the inverse of the unexported `shell_split` function: | ||
it takes a string or command object and escapes any special characters in such a way that calling | ||
`shell_split` on it would give back the array of words in the original command. The `special` | ||
keyword argument controls what characters in addition to whitespace, backslashes, quotes and | ||
dollar signs are considered to be special. Examples: | ||
dollar signs are considered to be special (default: none). Examples: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "in addition" isn't accurate any more then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is: whitespace, backslashes, quotes and dollar signs are always considered special; the |
||
|
||
julia> Base.shell_escape("cat", "/foo/bar baz", "&&", "echo", "done") | ||
"cat '/foo/bar baz' && echo done" | ||
|
||
julia> Base.shell_escape("echo", "this", "&&", "that") | ||
"echo this '&&' that" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no longer displays with quotes around the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make a PR to fix it. |
||
|
||
julia> Base.shell_escape("cat", "/foo/bar baz", "&&", "echo", "done", special="") | ||
"cat '/foo/bar baz' && echo done" | ||
""" | ||
shell_escape(args::AbstractString...; special::AbstractString=shell_special) = | ||
shell_escape(args::AbstractString...; special::AbstractString="") = | ||
sprint(io->print_shell_escaped(io, args..., special=special)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the leading spaces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
editor playing tricks on me?