Skip to content

Commit

Permalink
Make Base.shell_escape() and Base.shell_split() public (#53510)
Browse files Browse the repository at this point in the history
`Base.shell_escape()` is particularly useful since it's the only way
(AFAICT) to properly convert a `Cmd` to a string.
  • Loading branch information
JamesWrigley authored May 1, 2024
1 parent fe554b7 commit e25ce08
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
11 changes: 11 additions & 0 deletions base/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public
@locals,
@propagate_inbounds,

# External processes
shell_escape,
shell_split,
shell_escape_posixly,
shell_escape_csh,
shell_escape_wincmd,
escape_microsoft_c_args,

# Strings
escape_raw_string,

# IO
# types
BufferStream,
Expand Down
12 changes: 7 additions & 5 deletions base/shell.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ print_shell_escaped(io::IO; special::String="") = nothing
"""
shell_escape(args::Union{Cmd,AbstractString...}; special::AbstractString="")
The unexported `shell_escape` function is the inverse of the unexported `shell_split` function:
The unexported `shell_escape` function is the inverse of the unexported [`Base.shell_split()`](@ref) 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`
[`Base.shell_split()`](@ref) 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 (default: none).
Expand Down Expand Up @@ -284,6 +284,8 @@ The unexported `shell_escape_posixly` function
takes a string or command object and escapes any special characters in such a way that
it is safe to pass it as an argument to a posix shell.
See also: [`Base.shell_escape()`](@ref)
# Examples
```jldoctest
julia> Base.shell_escape_posixly("cat", "/foo/bar baz", "&&", "echo", "done")
Expand Down Expand Up @@ -316,7 +318,7 @@ a backslash.
This function should also work for a POSIX shell, except if the input
string contains a linefeed (`"\\n"`) character.
See also: [`shell_escape_posixly`](@ref)
See also: [`Base.shell_escape_posixly()`](@ref)
"""
function shell_escape_csh(io::IO, args::AbstractString...)
first = true
Expand Down Expand Up @@ -414,7 +416,7 @@ run(setenv(`cmd /C echo %cmdargs%`, "cmdargs" => cmdargs))
With an I/O stream parameter `io`, the result will be written there,
rather than returned as a string.
See also [`escape_microsoft_c_args`](@ref), [`shell_escape_posixly`](@ref).
See also [`Base.escape_microsoft_c_args()`](@ref), [`Base.shell_escape_posixly()`](@ref).
# Examples
```jldoctest
Expand Down Expand Up @@ -468,7 +470,7 @@ It joins command-line arguments to be passed to a Windows
C/C++/Julia application into a command line, escaping or quoting the
meta characters space, TAB, double quote and backslash where needed.
See also [`shell_escape_wincmd`](@ref), [`escape_raw_string`](@ref).
See also [`Base.shell_escape_wincmd()`](@ref), [`Base.escape_raw_string()`](@ref).
"""
function escape_microsoft_c_args(io::IO, args::AbstractString...)
# http://daviddeley.com/autohotkey/parameters/parameters.htm#WINCRULES
Expand Down
2 changes: 1 addition & 1 deletion base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ string literals. (It also happens to be the escaping convention
expected by the Microsoft C/C++ compiler runtime when it parses a
command-line string into the argv[] array.)
See also [`escape_string`](@ref).
See also [`Base.escape_string()`](@ref).
"""
function escape_raw_string(io::IO, str::AbstractString, delim::Char='"')
total = 0
Expand Down
6 changes: 6 additions & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ Base.Cmd
Base.setenv
Base.addenv
Base.withenv
Base.shell_escape
Base.shell_split
Base.shell_escape_posixly
Base.shell_escape_csh
Base.shell_escape_wincmd
Base.escape_microsoft_c_args
Base.setcpuaffinity
Base.pipeline(::Any, ::Any, ::Any, ::Any...)
Base.pipeline(::Base.AbstractCmd)
Expand Down

0 comments on commit e25ce08

Please sign in to comment.