TLDR;
With Tryparse.jl you can do
julia> Tryparse.tryparse(Int, "1 + 1")
2
julia> Tryparse.tryparse(Vector{Int}, "[1, 2]")
2-element Vector{Int64}:
1
2
and make things like
julia script.jl --opt1="[1, 2, 3]" --opt2="[1 2; 3 4]" --opt3="2^10"
or
julia script.jl "[1, 2, 3]" "[1 2; 3 4]" "2^10"
easily do what you want it to do (which is parsing those arguments and give you objects of the correct type). It works out of the box with ArgParse, ArgMacros and Comonicon. (See the README for examples.)
Why and what?
After writing functions for handling simple command line arguments for the umpteenth time, I decided to put together a small package. This is not a full blown parser, but just some lightweight way to make the following work as expected:
julia> Tryparse.tryparse(Int, "2^10 + 1")
1025
julia> Tryparse.tryparse(Vector{Matrix{Int}}, "[[2 2; 3 1], [1 2; 3 4]]")
2-element Vector{Matrix{Int64}}:
[2 2; 3 1]
[1 2; 3 4]
(For all supported types see the README.)
My main application is ergonomic command line argument parsing. You can do
using ArgParse, Tryparse
Tryparse.@override Int Vector{Int} # without additional argument, all types are intercepted
and then ArgParse will parse, e.g., --opt="2^10"
to the correct value. Similar for ArgMacros and Comonicon. (See the README for examples.)
Does this use eval
?
No, this package is certified organic and non-eval
.