# How to query the number of arguments passed to a CLI program?

**URL:** <https://discourse.julialang.org/t/how-to-query-the-number-of-arguments-passed-to-a-cli-program/121461>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 18, 2024, 3:56pm UTC](https://discourse.julialang.org/t/how-to-query-the-number-of-arguments-passed-to-a-cli-program/121461 "2024-10-18T15:56:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 18, 2024, 3:56pm UTC](https://discourse.julialang.org/t/how-to-query-the-number-of-arguments-passed-to-a-cli-program/121461/1 "2024-10-18T15:56:13Z")

</div>

I am trying to find a way to query the number of arguments passed to a Julia CLI program.

This is what I have come up with, but it doesn’t quite make sense. (Explanation below.)

```julia
function main()

    if size(ARGS) != 1
        println("Error: Invalid number of arguments, expected 1 arguments DATE")
        println("there were $(size(ARGS)) args")
        println(typeof(size(ARGS)))
        println(typeof(ARGS))
    end

    target_date::Union{Date, Nothing} = nothing

    for arg in ARGS
        println("arg: $(arg)")
        date::Date = Date(arg, dateformat"yyyymmdd")
        println("Running for date $(date)")
        target_date = date
    end
end

main()

```

This doesn’t work because `size(ARGS)` returns a `Tuple{Int64}`. Here’s the output:

```julia
there were (1,) args
Tuple{Int64}
Vector{String}

```

I find this surprising since a list of arguments passed to a CLI program can only be 1 dimensional. This is imposed by the Operating System, since it represents those arguments as an array of pointers to character strings.

I can of course “fix it” by simply taking the first element of the tuple before performing the comparison with the `if` statement. However, this feels like bad software engineering practice, because if I were to try to access `size(ARGS)[1]`, usually one should first check that this element exists. (In other words, check that `size` did not return an empty vector.)

But that is a lot of arguably unnecessary additional work.

Does anyone have any thoughts about this?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 18, 2024, 4:02pm UTC](https://discourse.julialang.org/t/how-to-query-the-number-of-arguments-passed-to-a-cli-program/121461/2 "2024-10-18T16:02:58Z")

</div>

you want `length(ARGS)`

---

<div class="post-metadata">

**Author:** ![anon91151494](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@anon91151494](https://discourse.julialang.org/u/anon91151494)\
**Post date:** [October 18, 2024, 5:13pm UTC](https://discourse.julialang.org/t/how-to-query-the-number-of-arguments-passed-to-a-cli-program/121461/3 "2024-10-18T17:13:33Z")

</div>

Ah of course, that makes sense. Thanks
