[ANN] Tryparse.jl: Parsing things for you


With Tryparse.jl you can do

julia> Tryparse.tryparse(Int, "1 + 1")

julia> Tryparse.tryparse(Vector{Int}, "[1, 2]")
2-element Vector{Int64}:

and make things like

julia script.jl --opt1="[1, 2, 3]" --opt2="[1 2; 3 4]" --opt3="2^10"


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")

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.


Why does this case, which is valid Julia syntax, output Nothing?

Tryparse.tryparse(Vector{Int}, "[1:2; 6:9]")
1 Like

Because so far I did not need this case during my CLI adventures. I will probably add it in the next version.

1 Like