Should we check if "dest"
is a directory, and if so, copy "src"
to that directory?
julia> mkdir("dest"); touch("src"); cp("src", "dest")
ERROR: ArgumentError: 'dest' exists. `force=true` is required to remove 'dest' before copying.
Stacktrace:
[1] #checkfor_mv_cp_cptree#10(::Bool, ::Function, ::String, ::String, ::String) at ./file.jl:191
[2] #checkfor_mv_cp_cptree at ./<missing>:0 [inlined]
[3] #cp#12(::Bool, ::Bool, ::Nothing, ::Function, ::String, ::String) at ./file.jl:241
[4] cp(::String, ::String) at ./file.jl:236
[5] top-level scope
Compare e.g. with
fredrik@fredrik-laptop:~$ mkdir dest && touch src && cp src dest && ls dest
src
Adding triage since this changes behavior and probably needs to be decided before 1.0.
What do other languages do? I would think it's reasonable to be stricter in a programming language than in the shell, which allows catching potential mistakes.
Just noting also that the current behaviour is not very useful; it is unlikely that you want to replace a folder with a file (which is the result of setting force = true
in the call above), and that, what you currently have to do
cp("src", joinpath(dir, "src"))
is not very nice.
Would cp(src, into=dir)
be a good API here?
I think changing this would be worse in terms of orthogonality --- if you actually want the logic "if this is a directory then copy into it", then you should write that. Conditional behavior should not be built into the library function itself.
cp(src, into=dir)
doesn't seem that much better than cp(src, joinpath(dir, src))
. (If somebody needs this function a lot they could always define a cpinto(src, dir) = cp(src, joinpath(dir, src))
function.)
This use-case would have to come up a lot to put this specific functionality in to Base. Grepping Julia packages for uses of cp
with joinpath
, I see a handful of code that could be marginally shortened with an into=
keyword, but it doesn't seem like enough to be worthwhile.
Most helpful comment
cp(src, into=dir)
doesn't seem that much better thancp(src, joinpath(dir, src))
. (If somebody needs this function a lot they could always define acpinto(src, dir) = cp(src, joinpath(dir, src))
function.)This use-case would have to come up a lot to put this specific functionality in to Base. Grepping Julia packages for uses of
cp
withjoinpath
, I see a handful of code that could be marginally shortened with aninto=
keyword, but it doesn't seem like enough to be worthwhile.