Catching empty ARGS

I’m trying to check if there are any ARGS had been sent from the command line, using the below:

@show ARGS
localARGS = if findfirst(ARGS) == nothing ["arg1", "arg2"] else ARGS end 

But got the below error:

ERROR: LoadError: syntax: space before “[” not allowed in “nothing [”

So, I recoded it as:

localARGS = if findfirst(ARGS) == nothing
                    ["arg1", "arg2"]
            else
                    ARGS
            end 

I got an error, that:

ERROR: LoadError: TypeError: non-boolean (String) used in boolean
context

I also tried:

name = (ARGS[1] == nothing ? "arg1" : ARGS[1])

But got the below error:

ERROR: LoadError: BoundsError: attempt to access 0-element
Array{String,1} at index [1]

And tried:

name = isdefined(:ARGS) ? ARGS[1] : "arg1"

But ended with below error:

ERROR: LoadError: ArgumentError: isdefined: too few arguments
(expected 2)

How about

name = isempty(ARGS) ? "arg1" : ARGS[1]

If you really want something like your first piece of code then a sprinkling of semi-colons is needed. (The extra brackets don’t appear to be needed but personally I’d have them in for reassurance.)

localARGS = ( if findfirst(ARGS) == nothing ; ["arg1", "arg2"] ; else ARGS ; end )

Though again, I’d replace the findfirst with isempty.

1 Like

This worked fine, thanks a lot.

This did not work when I send arguments to the file, I got the below error:

ERROR: LoadError: TypeError: non-boolean (String) used in boolean context

Ok, it appears findfirst has changed its behaviour between v0.6 and v1.0; on v0.7 findfirst(["hello"]) gives the deprecation warning

Warning: In the future `findnext` will only work on boolean collections. Use `findnext(x->x!=0, A, start)` instead.

As such, I’d stick with

localARGS = ( if isempty(ARGS) ; [“arg1”, “arg2”] ; else ARGS ; end )

I got

ERROR: LoadError: syntax: invalid character ““”

Fixed it to:

localARGS = ( if isempty(ARGS) ; ["arg1", "arg2"] ; else ARGS ; end )

Thanks