Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Extending the Sample Parser
You could alter or extend the sample parser to in several ways. For example, you could define individual CommandLineEntry subclasses for each type and eliminate the long Case structure in the ValidateValue method. You could create Exception classes inherited from ApplicationException to simplify the process of checking the errors. That would also make it easier to remove the error messages from the code and put them in localizable resource files. You could add a Number type that would convert the string entries to a designated numeric type and format, using a Min and Max property to verify that the entry lies within a specific range. The Min and Max properties would be useful for date types as well. Finally, as implemented, the parser doesn't fail immediately when it encounters a condition that causes an overall parse failure; instead, it simply adds error messages to the Errors collection. While that causes the parser to be slower when an error occurs early in the command line, it also gives developers the greatest possible amount of information about what the parser is doing. In addition, the sample code is not highly optimized—you can probably find numerous ways to make it faster.

A. Russell Jones is DevX's Executive Editor. He's a former reptile keeper and professional musician who now composes computer applications. His most recent books are Mastering ASP.NET with VB.NET and Mastering ASP.NET with Visual C# (both published by Sybex). Reach him by e-mail at rjones@devx.com.
Comment and Contribute






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



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