public override void Execute() { base.Execute(); List <string> singleOptionList = Base64Options.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, Base64Options.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartBase64(); } Terminate(); }
private static void CheckOptions(Base64CommandLineOptions options) { if (!options.IsSetHelp && !options.IsSetVersion) { if (!options.IsSetFile && !options.IsSetText) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a file or a text.")); } if (options.IsSetFile && options.IsSetText) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "can only specify a file or a text.")); } if (options.IsSetFile && string.IsNullOrEmpty(options.File)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "bad file path.")); } if (options.IsSetText && string.IsNullOrEmpty(options.Text)) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "bad text format.")); } } }
public override void Execute() { base.Execute(); List<string> singleOptionList = Base64Options.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, Base64Options.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartBase64(); } Terminate(); }
private static Base64CommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } Base64CommandLineOptions targetOptions = new Base64CommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { Base64OptionType optionType = Base64Options.GetOptionType(arg); if (optionType == Base64OptionType.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 Base64OptionType.Decode: targetOptions.IsSetDecode = true; break; case Base64OptionType.Encoding: targetOptions.IsSetEncoding = true; if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII") { targetOptions.Encoding = Encoding.ASCII; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7") { targetOptions.Encoding = Encoding.UTF7; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8") { targetOptions.Encoding = Encoding.UTF8; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE") { targetOptions.Encoding = Encoding.Unicode; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32") { targetOptions.Encoding = Encoding.UTF32; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE") { targetOptions.Encoding = Encoding.BigEndianUnicode; } else { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid encoding, support ASCII, UTF7, UTF8, UTF32, Unicode, BigEndianUnicode.")); } break; case Base64OptionType.Text: targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Arguments[arg]; break; case Base64OptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case Base64OptionType.Help: targetOptions.IsSetHelp = true; break; case Base64OptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetText) { targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Parameters.First(); } } return(targetOptions); }
private static Base64CommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); Base64CommandLineOptions targetOptions = new Base64CommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { Base64OptionType optionType = Base64Options.GetOptionType(arg); if (optionType == Base64OptionType.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 Base64OptionType.Decode: targetOptions.IsSetDecode = true; break; case Base64OptionType.Encoding: targetOptions.IsSetEncoding = true; if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII") { targetOptions.Encoding = Encoding.ASCII; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7") { targetOptions.Encoding = Encoding.UTF7; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8") { targetOptions.Encoding = Encoding.UTF8; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE") { targetOptions.Encoding = Encoding.Unicode; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32") { targetOptions.Encoding = Encoding.UTF32; } else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE") { targetOptions.Encoding = Encoding.BigEndianUnicode; } else { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid encoding, support ASCII, UTF7, UTF8, UTF32, Unicode, BigEndianUnicode.")); } break; case Base64OptionType.Text: targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Arguments[arg]; break; case Base64OptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case Base64OptionType.Help: targetOptions.IsSetHelp = true; break; case Base64OptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetText) { targetOptions.IsSetText = true; targetOptions.Text = commandLineOptions.Parameters.First(); } } return targetOptions; }