public override void Execute() { base.Execute(); List <string> singleOptionList = UniqueOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, UniqueOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartUnique(); } Terminate(); }
private static UniqueCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } UniqueCommandLineOptions targetOptions = new UniqueCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { UniqueOptionType optionType = UniqueOptions.GetOptionType(arg); if (optionType == UniqueOptionType.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 UniqueOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case UniqueOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case UniqueOptionType.Sort: targetOptions.IsSetSort = true; break; case UniqueOptionType.Help: targetOptions.IsSetHelp = true; break; case UniqueOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = commandLineOptions.Parameters.First(); } } return(targetOptions); }
private static void CheckOptions(UniqueCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (string.IsNullOrEmpty(checkedOptions.InputFile)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a file.")); } if (checkedOptions.IsSetOutputFile && string.IsNullOrEmpty(checkedOptions.OutputFile)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "bad output file path.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = UniqueOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, UniqueOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartUnique(); } Terminate(); }
private static UniqueCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); UniqueCommandLineOptions targetOptions = new UniqueCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { UniqueOptionType optionType = UniqueOptions.GetOptionType(arg); if (optionType == UniqueOptionType.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 UniqueOptionType.InputFile: targetOptions.InputFile = commandLineOptions.Arguments[arg]; break; case UniqueOptionType.OutputFile: targetOptions.IsSetOutputFile = true; targetOptions.OutputFile = commandLineOptions.Arguments[arg]; break; case UniqueOptionType.Sort: targetOptions.IsSetSort = true; break; case UniqueOptionType.Help: targetOptions.IsSetHelp = true; break; case UniqueOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (string.IsNullOrEmpty(targetOptions.InputFile)) { targetOptions.InputFile = commandLineOptions.Parameters.First(); } } return targetOptions; }