arcpy.GetArgumentCount() doesn???t count the correct number of arguments passed.

2059
3
05-02-2012 04:07 PM
BrianHickinbotham
New Contributor
I have a script that works perfect in python, but isn???t working correctly as a script tool in ArcCatalog.

I have 3 arcpy.GetParameterAsText arguments, two required and one optional.  When I run the script in python it
prints the correct number of arguments.  When I run it in the script tool it prints the count at 3, if I pass 2 or 3.  I
am trying to have it run some steps if the count is 2, and more steps if the count is 3.  The problem is that it returns the
count of 3 every time.

#Get the name and path of the Enterprise Geodatabase
ws = arcpy.GetParameterAsText(0)  #required   In the script tool this parameter is a Workspace
fullPathName = arcpy.GetParameterAsText(1)  #The full path name of the output file geodatabase (required)  In the script tool this parameter is a Workspace
litName = arcpy.GetParameterAsText(2) #Optional The three letter code for records in a field.  In the script tool this parameter is a String

pCount = arcpy.GetArgumentCount()
arcpy.AddMessage(pCount)

if pCount == 2:
    do things
else:
    do other things

The add messages shows a count of 3 every time.  If I can't get the GetArgumentCount() to return the correct number of passed arguments, I'm at a loss of what to do.
Tags (2)
0 Kudos
3 Replies
Luke_Pinner
MVP Regular Contributor
When run as a script tool, arcpy passes empty optional arguments as empty strings (i.e. '').
Test this with:
litName = arcpy.GetParameterAsText(2) #Optional The three letter code for records in a field. In the script tool this parameter is a String

pCount = arcpy.GetArgumentCount()

if pCount == 2 or not litName:
    do things
else:
    do other things

PS: please use CODE tags when posting code - http://forums.arcgis.com/threads/48475-Please-read-How-to-post-Python-code
0 Kudos
KimOllivier
Occasional Contributor III
Optional parameters are passed as a # character so that place counting is preserved.
So you could test for the value being == '#' to see if it was not set.
0 Kudos
Luke_Pinner
MVP Regular Contributor
Optional parameters are passed as a # character so that place counting is preserved.
So you could test for the value being == '#' to see if it was not set.


That's what I thought and what I remember from ArcGIS 9x, but it seems to be empty strings now not '#'.  Actually, I just tested this and it's an empy string '' when using GetParameterAsText(n) and '#' when using sys.argv

For a script tool with 3 parameters, 2 optional (and not entered when I ran it).
import sys,arcpy
for i,arg in enumerate(sys.argv[1:]):
    arcpy.AddMessage("argv %s=%s"%(i,repr(arg)))
    arcpy.AddMessage("param %s=%s"%(i,repr(arcpy.GetParameterAsText(i))))

Executing: Script Layer1 # #
Start Time: Mon May 14 12:33:51 2012
Running script Script...
argv 0='Layer1'
param 0=u'Layer1'
argv 1='#'
param 1=''
argv 2='#'
param 2=''
Completed script Script...
Succeeded at Mon May 14 12:33:52 2012 (Elapsed Time: 1.00 seconds)
0 Kudos