From 2211e0314ebec11a1d8272f3ed201cf9c6d3d771 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 28 Mar 2017 12:29:14 -0400 Subject: [PATCH 1/2] access Cmd elements as an array of strings --- base/process.jl | 8 ++++++++ doc/src/manual/running-external-programs.md | 12 ++++++++++++ test/spawn.jl | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/base/process.jl b/base/process.jl index cd15390ca42d6..1b2961ac625c3 100644 --- a/base/process.jl +++ b/base/process.jl @@ -805,3 +805,11 @@ wait(x::Process) = if !process_exited(x); stream_wait(x, x.exitnotify); end wait(x::ProcessChain) = for p in x.processes; wait(p); end show(io::IO, p::Process) = print(io, "Process(", p.cmd, ", ", process_status(p), ")") + +# allow the elements of the Cmd to be accessed as an array or iterator +for f in (:length, :endof, :start, :eachindex, :eltype, :first, :last) + @eval $f(cmd::Cmd) = $f(cmd.exec) +end +for f in (:next, :done, :getindex) + @eval $f(cmd::Cmd, i) = $f(cmd.exec, i) +end diff --git a/doc/src/manual/running-external-programs.md b/doc/src/manual/running-external-programs.md index a179d9f5d006b..dbeef5f23969f 100644 --- a/doc/src/manual/running-external-programs.md +++ b/doc/src/manual/running-external-programs.md @@ -60,6 +60,18 @@ julia> open(`less`, "w", STDOUT) do io 3 ``` +The program name and the individual arguments in a command can be accessed +and iterated over as if the command were an array of strings: +```jldoctest +julia> collect(`echo "foo bar"`) +2-element Array{String,1}: + "echo" + "foo bar" + +julia> `echo "foo bar"`[2] +"foo bar" +``` + ## [Interpolation](@id command-interpolation) Suppose you want to do something a bit more complicated and use the name of a file in the variable diff --git a/test/spawn.jl b/test/spawn.jl index 8b7582b191ad0..87d9bf169d85e 100644 --- a/test/spawn.jl +++ b/test/spawn.jl @@ -466,3 +466,14 @@ end Base.showerror(io::IO, e::Error19864) = print(io, "correct19864") throw(Error19864())'`), stderr=catcmd)) == "ERROR: correct19864" + +# accessing the command elements as an array or iterator: +let c = `ls -l "foo bar"` + @test collect(c) == ["ls", "-l", "foo bar"] + @test first(c) == "ls" == c[1] + @test last(c) == "foo bar" == c[3] == c[end] + @test c[1:2] == ["ls", "-l"] + @test eltype(c) == String + @test length(c) == 3 + @test eachindex(c) == 1:3 +end From 5560ea3c46188c588ff73f0b4f38a0c99ac73698 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 28 Mar 2017 12:33:31 -0400 Subject: [PATCH 2/2] whitespace --- doc/src/manual/running-external-programs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/manual/running-external-programs.md b/doc/src/manual/running-external-programs.md index dbeef5f23969f..36e624f9f08f1 100644 --- a/doc/src/manual/running-external-programs.md +++ b/doc/src/manual/running-external-programs.md @@ -65,7 +65,7 @@ and iterated over as if the command were an array of strings: ```jldoctest julia> collect(`echo "foo bar"`) 2-element Array{String,1}: - "echo" + "echo" "foo bar" julia> `echo "foo bar"`[2]