Just before the year ended, I discussed some additions you might want to make to the GrabAPicture application in the Improving the GrabAPicture Application (Part 1) post. One of those additions is allowing the user to modify the application configuration from the command line, rather than use the GUI to do it. Configuration modification is a common application requirement and any application that administrators have to support should have a command line option to make these modifications. You don’t have to provide access to absolutely every configuration option, but you should provide access to the most common configuration options. The options you support in your application is determined by:
- Administrator configuration needs: The administrator may be able to provide input on precisely which tasks the application will need to perform remotely. Remote tasks are usually good candidates for command line support.
Application complexity: When working with a simple application, you have the luxury of including all of the configuration options at the command line. As applications become more complex, you must pick and choose command line features. Using multiple levels of command line switches also becomes a requirement. As an example, look at the WMIC command line utility.
- Application type: The tasks that the application performs partially dictate the command line interface. An application designed for management tasks (such as WMIC) will require a more robust command line than one used for localized data handling (such as Notepad).
For the GrabAPicture application, the main consideration is application complexity. There are only two configuration settings, so the command line will support both of them. Of course, you have multiple options for adding the command line switches. Because the interface is relatively simple, we’ll change the command line arguments so they appear like this:
GrabAPicture /Picture:<Picture URI> [/Style:Stretched | Centered | Tiled]
GrabAPicture /Configure [/Local:[True | False]] [/Remote:[True | False]]
The new option, /Configure, displays the current configuration when used alone. Adding the /Local command line switch changes the Use Random Local Sources setting, while adding the /Remote command line switch changes the Use Random Remote Sources setting.
At this point, you can add the required functionality. Modifying the code is a three-step process:
- Change the help information.
- Add the configuration code.
- Add the frmMain-specific code required to perform tasks.
With these steps in mind, lets begin with the help information. The following listing shows the updated help code in bold.
As you can see, the updated code simply adds more help information to the existing help. The new help screen looks like this:
Now it’s time to add the code required to make the new switches work. This code appears directly before the /Picture command line switch code (the line that begins If CmdArg.Length > 8 Then in the Main() function):
This code begins by checking for the /Configuration command line switch. If the user has included it, then the code checks the number of command line arguments. When there is just one argument, the user has used the /Configuration command line switch alone, so the application displays the current configuration information. In order to perform this task, the code must create a GrabAPictureSettings object, CurrentSettings, and load the settings into it. At this point, displaying the information is simply a matter of accessing the current information in CurrentSettings. Make sure you convert the Boolean values to strings.
Setting the configuration is only a little harder. The first thing to consider is that you don’t know whether the user has included just the /Local or /Remote command line switches, or both of them. In addition, you have no idea of which order the user has used for the switches. As a consequence, this application relies on a For loop that doesn’t assume anything. It simply grabs the first switch and then processes it.
The command line switch is supposed to be followed by a colon (:) and then a value. Unfortunately, the user could provide anything as input. The code begins by verifying that the user has provided a switch and value combination (or at least the right number of arguments). It then checks for either /Local or /Remote. The value could still be incorrect, so the application uses the Boolean.TryParse() method to convert the input to a Boolean value safely. If everything works out correctly, the application makes the setting change and saves the result to disk by calling GrabAPictureSettings.SaveSettings(CurrentSettings).
With these changes, the application can now set the configuration from the command line. Next week you’ll see another new addition. In the meantime, let me know if you have any questions about this update at John@JohnMuellerBooks.com.