Access command line arguments in Julia
Asked Answered
B

5

61

When I type in

$ julia myprog.jl foo bar baz

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

I'm looking for the Python equivalent of sys.argv

Brattishing answered 11/1, 2014 at 0:35 Comment(0)
B
69

Ah, more web-searching led to the right answer. The keyword ARGS::Array{ASCIIString} holds command line arguments

Here is a simple example

# cli.jl

print(map(x->string(x, x), ARGS))  # Concatenate each arg onto itself and print

Lets test it at the command line:

$ julia cli.jl a b c
aa
bb
cc
Brattishing answered 11/1, 2014 at 0:59 Comment(0)
P
34

A simpler example:

#printargs.jl

println(ARGS[2]);

Run it as

julia printargs.jl a b c d

b

Note that the array index starts from 1 and NOT 0. Thus ARGS[2] prints b and not c as in case of many other programming languages.

Pecksniffian answered 16/4, 2015 at 2:1 Comment(0)
H
7

julia> Pkg.add("ArgParse") Docs at https://argparsejl.readthedocs.io/en/latest/argparse.html

Hann answered 6/10, 2019 at 23:31 Comment(0)
R
2

https://learn.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.

Robson answered 16/7, 2019 at 17:41 Comment(1)
Just found this: Base.JLOptions(). It shows a lot of detail of how Julia was launched.Robson
B
2

The command-line arguments are in the global constant ARGS of type Vector{String}. Note that ARGS's first element does not correspond to the name of the script. That is in the global PROGRAM_FILE::String.

As an example, to display each command-line argument in a separate line, you can iterate the ARGS vector and print each element:

for arg in ARGS
    println(arg)
end

or more concise by using foreach()X instead of for:

foreach(println, ARGS)

You can also use map() to produce other Vectors from ARGS:

capitalized_args = map(uppercasefirst, ARGS)
println(capitalized_args)

arg_lengths = map(length, ARGS)
println(arg_lengths)

Running this file, myprog.jl, with the command-line arguments foo, bar and buzz:

$ julia myprog.jl foo bar baz
["Foo", "Bar", "Buzz"]
[3, 3, 4]

XUsing map() instead of foreach() would have produced a (useless) Vector{Nothing} since println() returns the value nothing. In this scenario, we are only interested in the side effects of printing the elements when iterating over ARGS, so we use foreach() as it discards the value returned by the passed function. In other words, it is foreach(println, ARGS) :: Nothing but map(println, ARGS) :: Vector{Nothing}.

Bunting answered 30/1, 2023 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.