Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing a numeric vector #115

Open
fkrauer opened this issue Jan 14, 2022 · 1 comment
Open

Parsing a numeric vector #115

fkrauer opened this issue Jan 14, 2022 · 1 comment

Comments

@fkrauer
Copy link

fkrauer commented Jan 14, 2022

Hi
I have a script that takes some arguments, some of which are numeric vectors. Since the arguments passed to the command line need to be a string ("[0.1, 0.5]"), I am looking for ways to convert the string to a vector within the script. I found a solution if the string is a verbatim of julia syntax, but it does not work for something like [1.0:10.0:100.0;]. How can I parse such as vector? Here is a MWE (testparse.jl):

using ArgParse

s = ArgParseSettings()
@add_arg_table! s begin
    "--moo"
    arg_type = String
    "--foo"
    arg_type = String
end

args = parse_args(ARGS, s)

moo = Float64.(Meta.parse(args["moo"]).args)
println(moo)

foo = Float64.(Meta.parse(args["foo"]).args)
println(foo)

println(moo .* foo)

This works:

julia --project testparse.jl --foo="[1.0, 2.0, 3.0]" --moo="[1.0, 2.0, 3.0]"

But this doesn't work:

julia --project testparse.jl --foo="[1.0, 2.0, 3.0]" --moo="[1.0:10.0:100.0;]"
@stepanzh
Copy link

@fkrauer I'm not experienced with metaprogramming, but that seems like a solution

...
moo = float.(eval(Meta.parse(args["moo"])))

...
foo = float.(eval(Meta.parse(args["foo"])))
% j testparse.jl --foo="[1.0, 2.0, 3.0]" --moo="[4:4:12;]"
[4.0, 8.0, 12.0]
[1.0, 2.0, 3.0]
[4.0, 16.0, 36.0]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants