Add Ons - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Add Ons arrow Page 4 - Win Command of the Command Line Interface
Win Command of the Command Line Interface
By John Mueller

Rate This Article: Add This Article To:

Win Command of the Command Line Interface - Creating the Main Code
( Page 4 of 7 )

Creating the Main Code

It’s time to look at the main code for this application. What a command line application does is look for command line switches that control its operation. It then acts on those command line switches in a specific way. The example application normally builds a string that contains the requested date and time information, as shown in Listing 3.

ADVERTISEMENT

Listing 3: Processing the Command Line Switches

static void Main(string[] args)
{
   // Display help when requested.
   if ((args.Length == 0) || (args.Contains("/?")))
   {
      // Display the help screen, pause when in debug mode
      // and return.
      ShowHelp();
      CheckDebug();
      return;
   }

   // Contains the output.
   String Output = "";

   // Contains the current Date/Time.
   DateTime Current = DateTime.Now;

   // Begin checking for the other command line switches.
   for (int Count = 0; Count < args.Length; Count ++)
   {
      // Add a long date to the output string.
      if (args[Count].ToUpper() == "/LD")
         Output = Output + Current.ToLongDateString() + " ";
      // Add a long time to the output string.
      if (args[Count].ToUpper() == "/LT")
         Output = Output + Current.ToLongTimeString() + " ";
      // Add a short date to the output string.
      if (args[Count].ToUpper() == "/SD")
         Output = Output + Current.ToShortDateString() + " ";
      // Add a short time to the output string.
      if (args[Count].ToUpper() == "/ST")
         Output = Output + Current.ToShortTimeString() + " ";

      if (args[Count].ToUpper() == "/FD")
      {
         // Holds the formatting string.
         String TheFormat = args[Count + 1];

         // Define the output string.
         try
         {
            Output = Output + Current.ToString(TheFormat) + " ";
         }
         catch (FormatException FE)
         {
            Output = FE.Message;
         }
      }
   }

   // Check for an empty output string. If the string is empty
   // show the help screen.
   if (Output.Length == 0)
      ShowHelp();
   else
      Console.WriteLine(Output);

   // Pause for debugging.
   CheckDebug();

   return;
}

The example begins with the Main() method. This method receives an array of strings in args. Whenever the user types a space at the command line, it creates a new args string. Consequently, if the user types ShowDT /LD /LT at the command line and presses Enter, the application sees two arguments, /LD and /LT. The exception to the rule is delimited arguments. For example, if the user types ShowDT /FD "dd MMM yyyy" and presses Enter, the application only sees two arguments, /FD and dd MMM yyyy. The double quotes act as delimiters for the date/time string. You can also use single quotes as delimiters.

The first task is to determine whether the user has supplied any command line switches at all, or has supplied the /? command line switch. In either case, the application calls ShowHelp() to display the help screen described earlier in the article. It then calls CheckDebug() to place the pause shown in Figure 1 during debugging. Finally, it returns, because you don’t want to process any other command line arguments after displaying help.

When the application detects there are one or more command line arguments, it enters into loop to process them. This way, if the user supplies the formatted output requests in a certain order, they appear in that order in the output. Notice that the code uses args[Count].ToUpper() to extract the current command line switch. This approach ensures that the user can enter the command line switches in upper, lower, or mixed case as desired. Some command line utilities are case sensitive, but the normal approach is to use case insensitive command line switches.

Many of the command line switches for this example are simple. If you want to display the long date, then all you need to do is call the ToLongDateString() method and store the result in a string. The example processes all of the simple formats first and builds a string that contains the requested date and time information.

Processing the /FD command line switch isn’t as easy as processing the other command line switches. First, you must obtain the format string by peaking at the next argument in the args array. There shouldn’t be a problem in using this approach because the next argument won’t match any of the command line switches. However, interactions are always a problem with command line applications and you need to exercise care in peeking at the wrong time. The next step is to create an output string that contains a date and/or time in the specified format. However, you don’t know whether the user has supplied a valid format string or has supplied a format string at all. Consequently, you must perform this task within a try…catch block.

The final section of code checks to determine whether Output has any information in it. If not, the user hasn’t supplied any valid command line switches, so the application displays the help screen. Otherwise, the user has supplied valid information and the application displays the results on screen.



 
 
>>> More Add Ons Articles          >>> More By John Mueller
 



HD VOIP Has Arrived (with Tony Konstner)

Play Video >

All Videos >

Google and blonde jokes?

Read now >

Favorite books!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.