You can choose from several common argument-handling approaches. Ideally, you want to make argument entries simple and intuitive for the user and ensure accuracy. The argument data you input into the script might dictate your best approach. If you're not sure whether the argument approach you've chosen makes sense and is intuitive, give the script to some test users and get their feedback. Such testers can quickly point out easy changes. Here are three basic types of argument-handling approaches.
- Put your arguments in order. Simply ordering the arguments specifies what the arguments represent. Many resource kit and third-party utilities receive their input from specifically ordered arguments. An example of this approach would be a script that uses Robocopy to copy files from one location to another. The logical order would be source location, destination location, then log file location. If you were to reverse the source and destination locations, the destination files would overwrite the source files.
- Label the arguments. For example, you can use labels so that a script understands that an argument preceded by an /S: means that the string that follows is the source location and that an argument preceded by a /D: means that the string that follows is the destination location. Labeling prevents the potential sequencing error in the previous Robocopy example. If you use this method, you'll need to implement some code to capture the argument and remove the label information. I give you an example of this script routine in Web Listing 1.
- In some scripts, all arguments might be of equal value and might be merely a means to bring a list of similar objects into the script. An example of this type of script is a Ping script that pings one or more server-name arguments. If you use this method, you'll need to implement some special code to loop through all the arguments and run the routine against each of the listed items. I provide an example of this script routine in Listing 4, which pings a list of server names used as arguments.