# \[ANN\] Tryparse.jl: Parsing things for you

**URL:** <https://discourse.julialang.org/t/ann-tryparse-jl-parsing-things-for-you/107805>\
**Category:** Package Announcements\
**Tags:** parsing\
**Created:** [December 19, 2023, 6:47am UTC](https://discourse.julialang.org/t/ann-tryparse-jl-parsing-things-for-you/107805 "2023-12-19T06:47:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [December 19, 2023, 6:47am UTC](https://discourse.julialang.org/t/ann-tryparse-jl-parsing-things-for-you/107805/1 "2023-12-19T06:47:43Z")

</div>

TLDR;

With Tryparse.jl you can do

```julia-repl
julia> Tryparse.tryparse(Int, "1 + 1")
2

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

```

and make things like

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

```

or

```bash
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](https://github.com/thofma/Tryparse.jl?tab=readme-ov-file#command-line-argument-parsing) 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-repl
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](https://github.com/thofma/Tryparse.jl?tab=readme-ov-file#usage).)

My main application is ergonomic command line argument parsing. You can do

```julia-auto
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](https://github.com/thofma/Tryparse.jl?tab=readme-ov-file#command-line-argument-parsing) for examples.)

### Does this use `eval`?

No, this package is certified organic and non-`eval`.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 19, 2023, 9:17am UTC](https://discourse.julialang.org/t/ann-tryparse-jl-parsing-things-for-you/107805/2 "2023-12-19T09:17:53Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [December 19, 2023, 9:33am UTC](https://discourse.julialang.org/t/ann-tryparse-jl-parsing-things-for-you/107805/3 "2023-12-19T09:33:17Z")

</div>

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