Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Parse and Validate Command Line Parameters with VB.NET  : Page 3

Now that you can build console applications with VB.NET, you also need to parse and validate command line arguments. Use this CommandLineParser assembly to define and validate command line arguments and minimize the need to write custom parsing code.


advertisement
Setting Up the Sample Parser
The sample project CommandLineParserTest included with the downloadable code shows how to use the parser. First, add a reference to the CommandLineParse namespace to your test application and then create a CommandLineParser instance:

' At the top of the file Imports CommandLineParse Dim parser As CommandLineParser parser = New CommandLineParser(Command())

Next, populate the parser's CommandLineItemCollection by creating CommandLineItems and setting their properties. For example, the following method creates two CommandLineItems that specify a flag type entry (-f) followed by an ExistingFile type entry. For example, the command line would parse successfully if the file exists. In contrast, the parser would fail if the user enters a filename that doesn't exist, or fails to enter both parameters, or enters them out of order.

Sub SetupCommandLineEntries(ByVal parser _ As CommandLineParser) Dim anEntry As CommandLineEntry parser.Errors.Clear() parser.Entries.Clear() ' create a flag type entry that accepts a -f (file) ' flag, (meaning the next parameter is a file ' name), and is required anEntry = parser.CreateEntry _ (CommandLineParse.CommandTypeEnum.Flag, "f") anEntry.Required = True parser.Entries.Add(anEntry) ' store the new Entry in a local reference ' for use with the next CommandLineEntry's ' MustFollow property. Dim fileEntry As CommandLineEntry fileEntry = anEntry ' now create am ExistingFile type entry that must ' follow the -f flag. anEntry = parser.CreateEntry _ (CommandTypeEnum.ExistingFile) anEntry.MustFollowEntry = fileEntry anEntry.Required = True parser.Entries.Add(anEntry) End Sub

In this scenario, both entries are required and the ExistingFile entry must follow the flag entry, regardless of how many other parameters you may later create that precede or follow these two. After setting up the CommandLineItemCollection, call the CommandLineParser.Parse method to initiate the parse and validation (see Listing 1).

The Parse method returns a Boolean value with the overall result of the parse operation.



If parser.Parse() Then Console.WriteLine("Successful parse") Console.WriteLine("") Else Console.WriteLine("Parse failed") For Each sErr In parser.Errors Console.WriteLine("Reason: " & sErr) Next Console.WriteLine("") End If

Figure 2 shows the sample CommandLineParserTest application results after a successful request using the CommandLineItems described above and the command line -f c:\temp\junk.txt.

Figure 2: Application results after a successful request using the command line -f c:\temp\junk.txt.
In contrast, if you give the parser an invalid command line, such as -f c:\BadFile.txt, where the file doesn't exist (see Figure 3), or any other invalid parameters, such as -x c:\temp\junk.txt (see Figure 4), the Parse method returns False, and you can see the errors that accumulated during the parse operation as well as any unmatched parameters in the sample application's display.

Figure 3: Results after failed parse—an ExistingFile parameter type could not find the specified file.
Figure 4: Results after failed parse—user entered an invalid flag (-x rather than -f).


To use the parser, add a reference to the CommandLineParse namespace to your project, create a CommandLineParser instance, and then create CommandLineEntry instances for each possible entry type you want users to be able to enter on the command line. For each entry, you must minimally specify the CommandTypeEnum and pass a reference to the parser.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date