public override void Execute() { base.Execute(); List <string> singleOptionList = RemoveOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, RemoveOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartRemove(); } Terminate(); }
private static RemoveCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } RemoveCommandLineOptions targetOptions = new RemoveCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { RemoveOptionType optionType = RemoveOptions.GetOptionType(arg); if (optionType == RemoveOptionType.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 RemoveOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case RemoveOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case RemoveOptionType.Help: targetOptions.IsSetHelp = true; break; case RemoveOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { foreach (var item in commandLineOptions.Parameters) { targetOptions.Files.Add(item); } } return(targetOptions); }
private static void CheckOptions(RemoveCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (checkedOptions.Files.Count <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a file to be removed.")); } if (checkedOptions.IsSetDirectory && string.IsNullOrEmpty(checkedOptions.Directory)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "bad directory path.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = RemoveOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, RemoveOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartRemove(); } Terminate(); }
private static RemoveCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); RemoveCommandLineOptions targetOptions = new RemoveCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { RemoveOptionType optionType = RemoveOptions.GetOptionType(arg); if (optionType == RemoveOptionType.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 RemoveOptionType.Directory: targetOptions.IsSetDirectory = true; targetOptions.Directory = commandLineOptions.Arguments[arg]; break; case RemoveOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case RemoveOptionType.Help: targetOptions.IsSetHelp = true; break; case RemoveOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { foreach (var item in commandLineOptions.Parameters) { targetOptions.Files.Add(item); } } return targetOptions; }