public override void Execute() { base.Execute(); List <string> singleOptionList = CountOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, CountOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartCount(); } Terminate(); }
private static CountCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } CountCommandLineOptions targetOptions = new CountCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { CountOptionType optionType = CountOptions.GetOptionType(arg); if (optionType == CountOptionType.None) { throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg))); } switch (optionType) { case CountOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case CountOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case CountOptionType.Help: targetOptions.IsSetHelp = true; break; case CountOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetDirectory) { targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Parameters.First(); } } return(targetOptions); }
private static void CheckOptions(CountCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (!checkedOptions.IsSetDirectory || string.IsNullOrEmpty(checkedOptions.Directory)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a directory.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = CountOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, CountOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartCount(); } Terminate(); }
private static CountCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); CountCommandLineOptions targetOptions = new CountCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { CountOptionType optionType = CountOptions.GetOptionType(arg); if (optionType == CountOptionType.None) throw new CommandLineException( string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", string.Format(CultureInfo.CurrentCulture, "cannot parse the command line argument : [{0}].", arg))); switch (optionType) { case CountOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case CountOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case CountOptionType.Help: targetOptions.IsSetHelp = true; break; case CountOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetDirectory) { targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Parameters.First(); } } return targetOptions; }