diff --git a/README.md b/README.md index 0b1fbe89f..c65c8da8c 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,8 @@ Currently, the `@compat` macro supports the following syntaxes: * `@__MODULE__` is aliased to `current_module()` for Julia versions 0.6 and below. Versions of `Base.binding_module`, `expand`, `macroexpand`, and `include_string` were added that accept a module as the first argument. ([#22064]) +* `Cmd` elements can be accessed as if the `Cmd` were an array of strings for 0.6 and below ([#21197]). + * `Val(x)` constructs `Val{x}()`. ([#22475]) * `chol` and `chol!` for `UniformScalings` ([#22633]). diff --git a/src/Compat.jl b/src/Compat.jl index 7b596cbd0..5679e6c3e 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -601,6 +601,17 @@ else const collect = Base.collect end +# https://github.com/JuliaLang/julia/pull/21197 +if VERSION < v"0.7.0-DEV.257" + # 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 Base.$f(cmd::Cmd) = $f(cmd.exec) + end + for f in (:next, :done, :getindex) + @eval Base.$f(cmd::Cmd, i) = $f(cmd.exec, i) + end +end + # https://github.com/JuliaLang/julia/pull/21378 if VERSION < v"0.6.0-pre.beta.455" import Base: ==, isless diff --git a/test/runtests.jl b/test/runtests.jl index ddb5b6b58..45aed189f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1882,6 +1882,17 @@ let @test_throws MethodError Dates.Month(1) < Dates.Day(1) end +# PR #21197 +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 + # PR 22629 @test logdet(0.5) == log(det(0.5))