public override void Execute() { base.Execute(); List <string> singleOptionList = RenameMediaOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, RenameMediaOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartRename(); } Terminate(); }
private static RenameMediaCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } RenameMediaCommandLineOptions targetOptions = new RenameMediaCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { RenameMediaOptionType optionType = RenameMediaOptions.GetOptionType(arg); if (optionType == RenameMediaOptionType.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 RenameMediaOptionType.RegexPattern: targetOptions.RegexPattern = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.InputDirectory: targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case RenameMediaOptionType.Prefix: targetOptions.Prefix = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.Help: targetOptions.IsSetHelp = true; break; case RenameMediaOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return(targetOptions); }
private static void CheckOptions(RenameMediaCommandLineOptions checkedOptions) { if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion) { if (string.IsNullOrEmpty(checkedOptions.RegexPattern)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a regex pattern.")); } if (string.IsNullOrEmpty(checkedOptions.InputDirectory)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a input directory.")); } if (!Directory.Exists(checkedOptions.InputDirectory)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "no such input directory.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = RenameMediaOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, RenameMediaOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartRename(); } Terminate(); }
private static RenameMediaCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); RenameMediaCommandLineOptions targetOptions = new RenameMediaCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { RenameMediaOptionType optionType = RenameMediaOptions.GetOptionType(arg); if (optionType == RenameMediaOptionType.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 RenameMediaOptionType.RegexPattern: targetOptions.RegexPattern = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.InputDirectory: targetOptions.InputDirectory = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.Recursive: targetOptions.IsSetRecursive = true; break; case RenameMediaOptionType.Prefix: targetOptions.Prefix = commandLineOptions.Arguments[arg]; break; case RenameMediaOptionType.Help: targetOptions.IsSetHelp = true; break; case RenameMediaOptionType.Version: targetOptions.IsSetVersion = true; break; } } } return targetOptions; }