Best practice for dealing with Optional Parameters in Script Tools?

2740
14
Jump to solution
10-26-2021 11:09 AM
ZacharyHart
Occasional Contributor III

Here's a crude example to at least put forth some effort in asking this question. I tried searching and got some results but I fear my inquiry is too simplistic to find viable results.

Let's say we have just two parameters defined in the code; and in the script tool we have configured parameter 1 to be 'required' and parameter 2 to be 'optional'.

 

 

RequiredParameter = arcpy.GetParameterAsText(0)
OptionalParameter = arcpy.GetParameterAsText(1)

 

 

So if your code has a line expecting OptionalParameter, the script will fail. So how best to evaluate it being supplied by the user or not?

A few options I have found:

Evaluate for presence of the optional parameter:

 

 

if OptionalParameter and OptionalParameter != "#":
    #Do something with optional parameter

 

 

Try-Except block:

 

 

try:
    #arcpy tool using the supplied optional parameter
except:
    pass #move on with life never knowing if anything went wrong or why it did

 

 

 I suppose something could be done within the script tool validation, but the code itself would have to be modified as well.

Pitfalls of either? Better options exist?

0 Kudos
2 Solutions

Accepted Solutions
by Anonymous User
Not applicable

Ok,  I use this style for testing if the parameters from the tool's UI has been set, or set it to a default- (very useful for maintaining the scripts that are meant for standalone execution and as a script tool). Though, you may not even need it since it will either be a value or None and the conditional later on in your code would determine the course of action, but it is explicitly setting the variable.

 

 

 

isOptional = arcpy.GetParameterAsText(1)
if isOptional == '' or isOptional is None:
    isOptional = None

 

View solution in original post

BlakeTerhune
MVP Regular Contributor

I'll just toss out that you could also simply check for "truthiness"

 

if isOptional:
    # isOptional has a value.
    # do a thing.
else:
    # isOptional has no value.
    # handle it.

 

View solution in original post

14 Replies
BlakeTerhune
MVP Regular Contributor

I would say you should find a way to use a default value if OptionalParameter is not defined and write your code logic in a way that can handle any valid value (including the default).

ZacharyHart
Occasional Contributor III

I was thinking something similar but don't know how to implement it exactly. But the trick here is that if a user supplies no value, that is actually valid because they don't want to employ this parameter and this is the most likely scenario. Follow me?

0 Kudos
by Anonymous User
Not applicable

So keep it assigned to none and in your code where you need that parameter, split your process to execute either option. 

if optionalparameter is not none:

   Use it

else:

   Don’t use it.

ZacharyHart
Occasional Contributor III

Ok I see where you are going!

I've never set a default to none. Is none a valid input for default  or something like "" needs to be set?

0 Kudos
by Anonymous User
Not applicable

Something like this:

Optionalparam = arcpy.getparam…(1)

if Optionalparam = “” or Optionalparam is None:

    Optionalparam = None

sorry, I’m on my phone and it doesn’t do the code syntax window very well. I’ll fix this tonight.

 

ZacharyHart
Occasional Contributor III

Hah! So am I, many thanks! What ok curious about is exactly how to set the default to none in the script tool parameter dialogue...I can't believe I've never done that before...

0 Kudos
by Anonymous User
Not applicable

Just leave it blank and it will pass None if nothing is entered through the tool UI.

by Anonymous User
Not applicable

Ok,  I use this style for testing if the parameters from the tool's UI has been set, or set it to a default- (very useful for maintaining the scripts that are meant for standalone execution and as a script tool). Though, you may not even need it since it will either be a value or None and the conditional later on in your code would determine the course of action, but it is explicitly setting the variable.

 

 

 

isOptional = arcpy.GetParameterAsText(1)
if isOptional == '' or isOptional is None:
    isOptional = None

 

BlakeTerhune
MVP Regular Contributor

I'll just toss out that you could also simply check for "truthiness"

 

if isOptional:
    # isOptional has a value.
    # do a thing.
else:
    # isOptional has no value.
    # handle it.