Access command line arguments in Julia

2019-03-17 08:01发布

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

标签: julia
2条回答
家丑人穷心不美
2楼-- · 2019-03-17 08:27

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.

查看更多
祖国的老花朵
3楼-- · 2019-03-17 08:36

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
查看更多
登录 后发表回答