December 2007
Intermediate to advanced
896 pages
19h 57m
English
You require your applications to accept one or more command-line parameters in a standard format. You need to access and parse the entire command line passed to your application.
In Example 3-7, use the following classes together to help with parsing command-line parameters: Argument, ArgumentDefinition, and ArgumentSemanticAnalyzer.
Example 3-7. Argument class
using System; using System.Diagnostics; using System.Linq; using System.Collections.ObjectModel; public sealed class Argument { public string Original { get; private set; } public string Switch { get; private set; } public ReadOnlyCollection<string> SubArguments { get; private set; } private List<string> subArguments; public Argument(string original) { Original = original; Switch = string.Empty; subArguments = new List<string>( ); SubArguments = new ReadOnlyCollection<string>(subArguments); Parse( ); } private void Parse( ) { if (string.IsNullOrEmpty(Original)) { return; } char[] switchChars = { '/', '-' }; if (!switchChars.Contains(Original[0])) { return; } string switchString = Original.Substring(1); string subArgsString = string.Empty; int colon = switchString.IndexOf(':'); if (colon >= 0) { subArgsString = switchString.Substring(colon + 1); switchString = switchString.Substring(0, colon); } Switch = switchString; if (!string.IsNullOrEmpty(subArgsString)) subArguments.AddRange(subArgsString.Split(';')); } public bool IsSimple { get { return SubArguments.Count == 0; } } ...Read now
Unlock full access