Win Command of the Command Line Interface - Creating the Console Example (
Page 2 of 7 )
Creating the Console Example
After you’ve worked at the command line for a while, you discover most of the commands don’t have a GUI—you type a command and they display the output as text. This kind of application is a console application—one that lacks a GUI. Creating a console application is one of the easiest ways to discover the command line. The example in this section displays the current date and/or time in the format you specify. I actually use this utility when I work at the command line. I use it to add a date and time stamp to files I create for other purposes. I’ll show you one way in which I use it after we look at the example. Interestingly enough, there isn’t a way to perform this task easily using the existing commands, so you’ll end with a new tool for your command line toolbox. (You can use the Time /T command to output the time, but you have no choice in formatting that output; likewise with the Date /T command.)
ADVERTISEMENT
Before you begin creating a command line application, you want to define the list of commands it will perform. This process is akin to designing the user interface for a GUI application, except you work with text commands. This example uses the following command line switches.
/? - Displays the help screen.
/FD DateTimeString - Displays the date and/or time using the specified formatting characters.
/LD - Displays the date in long format.
/LT - Displays the time in long format.
/SD - Displays the date in short format.
/ST - Displays the time in short format.
/UTC - Displays the date and time in Coordinated Universal Time format.
The name of this example is ShowDT. To begin, choose the Console Application project in the New Project dialog box in Visual Studio. A console application begins with a very small Main() method and that’s about it. The Main() method receives an array of strings that tells you which command line arguments the user supplied at the command line.
Defining the Help Screen
Every console application will have at least two cases. The user can ask for help or the user can execute a command. For this example, when the user supplied either the /? or doesn’t supply any command line arguments at all, the application displays a help screen. Otherwise, the application searches for valid command line arguments. As a safety feature, this application displays the help screen when the user doesn’t supply any valid command line switches. Because you’ll display the help screen a number of times, it’s better to place the information in its own method. Listing 1 shows the help screen code for this example.
Listing 1: Providing a Help Screen
static void ShowHelp()
{
Console.Write("Displays the date and/or time in the format");
Console.WriteLine("you request.");
Console.WriteLine();
Console.Write("ShowDT [/?][/FD <DateTimeString>][/LD][/LT]");
Console.WriteLine("[/SD][/ST][/UTC]");
Console.WriteLine();
Console.WriteLine("/? - Displays the help screen.");
Console.Write("/FD DateTimeString - Displays the date and/or");
Console.WriteLine("time using the specified");
Console.WriteLine(" formatting characters.");
Console.WriteLine("/LD - Displays the date in long format.");
Console.WriteLine("/LT - Displays the time in long format.");
Console.WriteLine("/SD - Displays the date in short format.");
Console.WriteLine("/ST - Displays the time in short format.");
Console.Write("/UTC - Displays the date and time in Coordinated");
Console.WriteLine("Universal Time format.");
return;
}
As you can see, there isn’t anything too complicated here. I did format the help text for a nice appearance in the output, as shown in Figure 1. It’s standard practice to provide a minimum of three pieces of information as part of your help screen: a summary of the purpose of the command line application, the command line syntax, and an explanation of the usage of the command line switches.
Figure 1: Every command line application requires a help screen accessed with the /? command line switch.
As your command line application becomes more complex, you should add a fourth section that shows usage examples. Some command line applications become even more complex and rely on multiple help screens to accomplish work. The WMIC utility is an example of such a command line application. Type WMIC /?, press Enter, and you see one help screen. Type WMIC /NAMESPACE /?, press Enter, and you see a different, more detailed, help screen. In short, you need to add enough help to your application to ensure that people can use it successfully.