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?
Solved! Go to Solution.
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
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.
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).
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?
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.
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?
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.
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...
Just leave it blank and it will pass None if nothing is entered through the tool UI.
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
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.