Access command line arguments in Julia

后端 未结 4 1592
别那么骄傲
别那么骄傲 2021-02-05 00:34

When I type in

$ julia myprog.jl foo bar baz

Where in my code can I go to access the strings \"foo\", \"bar\", \"baz\" ?

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 00:53

    https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getcommandlinea

    In the case that you really need the exact args that came to julia including the -e, there is a workaround for Windows. Looking at /proc/PID/cmdline you can extract it linux. Mac doesn't have the same /proc option, so asking ps works nicely.

    if Sys.iswindows()
        mycmd = unsafe_string(ccall(:GetCommandLineA, Cstring, ()))
    elseif Sys.isapple()
        mycmd = strip(read(`/bin/ps -p $(getpid()) -o command=`, String))
    elseif Sys.isunix()
        mycmd = replace(read(joinpath("/", "proc", string(getpid()), "cmdline"), String), "\x00"=>" ")
    else
        mycmd = string(Base.julia_cmd()) * join(map(x->" " * x, ARGS))
    end
    

    But typical use cases you only need to look at ARGS.

提交回复
热议问题