Accessing parameters in a script tool

The illustration below shows a script tool dialog box with three parameters: an input workspace, a clip feature class, and an output workspace. All feature classes in the input workspace are clipped to the clip feature class (using the Clip tool) and written to the output workspace.

Script tool parameters

In the illustration above, once parameter values have been entered on the tool dialog box and the OK button is clicked, the script reads the values of the parameters using GetParameterAsText, as follows:

import arcpy

# Read the parameter values:
#  1: input workspace
#  2: input clip features
#  3: output workspace
#
in_workspace = arcpy.GetParameterAsText(0)
clip_features = arcpy.GetParameterAsText(1)
out_workspace = arcpy.GetParameterAsText(2)
arcpy.env.workspace = in_workspace

sys.argv

In Python, sys.argv provides an alternative for reading parameter values. The example above could be rewritten to use sys.argv as below. Note that the index of the arguments change from GetParameterAsText. This is because the 0-index (sys.argv[0]) returns the script file path and name.

import arcpy
import sys

# Read the parameter values:
#  1: input workspace
#  2: input clip features
#  3: output workspace
#
in_workspace = sys.argv[1]
clip_features = sys.argv[2]
out_workspace = sys.argv[3]
arcpy.env.workspace = in_workspace

sys.argv has limitations on the number of characters it can accept. GetParameterAsText has no character limit. For this reason alone, it is recommended that you use GetParameterAsText.

関連トピック


このトピックの内容
  1. sys.argv