Пример #1
0
        public override void Execute()
        {
            base.Execute();

            List <string>      singleOptionList = EncodeOptions.GetSingleOptions();
            CommandLineOptions cloptions        = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray());

            options = ParseOptions(cloptions);
            CheckOptions(options);

            if (options.IsSetHelp)
            {
                RaiseCommandLineUsage(this, EncodeOptions.Usage);
            }
            else if (options.IsSetVersion)
            {
                RaiseCommandLineUsage(this, Version);
            }
            else
            {
                StartEncode();
            }

            Terminate();
        }
Пример #2
0
 private static void CheckOptions(EncodeCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (!checkedOptions.IsSetInputFile && !checkedOptions.IsSetDirectory)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a input file or a directory."));
         }
         if (!checkedOptions.IsSetFromEncoding)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify the input file current encoding."));
         }
         if (!checkedOptions.IsSetToEncoding)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify the output file target encoding."));
         }
         if (checkedOptions.IsSetInputFile && WildcardCharacterHelper.IsContainsWildcard(checkedOptions.InputFile) &&
             checkedOptions.IsSetOutputFile)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "output file path has been set, so can only set one input file."));
         }
         if (checkedOptions.IsSetDirectory && checkedOptions.IsSetOutputFile)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "output file path has been set, so can not set a input directory."));
         }
     }
 }
Пример #3
0
        public override void Execute()
        {
            base.Execute();

              List<string> singleOptionList = EncodeOptions.GetSingleOptions();
              CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray<string>(), singleOptionList.ToArray());
              options = ParseOptions(cloptions);
              CheckOptions(options);

              if (options.IsSetHelp)
              {
            RaiseCommandLineUsage(this, EncodeOptions.Usage);
              }
              else if (options.IsSetVersion)
              {
            RaiseCommandLineUsage(this, Version);
              }
              else
              {
            StartEncode();
              }

              Terminate();
        }
Пример #4
0
        private static EncodeCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            EncodeCommandLineOptions targetOptions = new EncodeCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    EncodeOptionType optionType = EncodeOptions.GetOptionType(arg);
                    if (optionType == EncodeOptionType.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 EncodeOptionType.InputFile:
                        targetOptions.IsSetInputFile = true;
                        targetOptions.InputFile      = commandLineOptions.Arguments[arg];
                        break;

                    case EncodeOptionType.OutputFile:
                        targetOptions.IsSetOutputFile = true;
                        targetOptions.OutputFile      = commandLineOptions.Arguments[arg];
                        break;

                    case EncodeOptionType.FromEncoding:
                        targetOptions.IsSetFromEncoding = true;
                        if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII")
                        {
                            targetOptions.FromEncoding = Encoding.ASCII;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7")
                        {
                            targetOptions.FromEncoding = Encoding.UTF7;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8")
                        {
                            targetOptions.FromEncoding = Encoding.UTF8;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE")
                        {
                            targetOptions.FromEncoding = Encoding.Unicode;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32")
                        {
                            targetOptions.FromEncoding = Encoding.UTF32;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE")
                        {
                            targetOptions.FromEncoding = 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 EncodeOptionType.ToEncoding:
                        targetOptions.IsSetToEncoding = true;
                        if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII")
                        {
                            targetOptions.ToEncoding = Encoding.ASCII;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7")
                        {
                            targetOptions.ToEncoding = Encoding.UTF7;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8")
                        {
                            targetOptions.ToEncoding = Encoding.UTF8;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE")
                        {
                            targetOptions.ToEncoding = Encoding.Unicode;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32")
                        {
                            targetOptions.ToEncoding = Encoding.UTF32;
                        }
                        else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE")
                        {
                            targetOptions.ToEncoding = 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 EncodeOptionType.Directory:
                        targetOptions.IsSetDirectory = true;
                        targetOptions.Directory      = commandLineOptions.Arguments[arg];
                        break;

                    case EncodeOptionType.Recursive:
                        targetOptions.IsSetRecursive = true;
                        break;

                    case EncodeOptionType.Help:
                        targetOptions.IsSetHelp = true;
                        break;

                    case EncodeOptionType.Version:
                        targetOptions.IsSetVersion = true;
                        break;
                    }
                }
            }

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (!targetOptions.IsSetInputFile)
                {
                    targetOptions.IsSetInputFile = true;
                    targetOptions.InputFile      = commandLineOptions.Parameters.First();
                }
            }

            return(targetOptions);
        }
Пример #5
0
 private static void CheckOptions(EncodeCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
       {
     if (!checkedOptions.IsSetInputFile && !checkedOptions.IsSetDirectory)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify a input file or a directory."));
     }
     if (!checkedOptions.IsSetFromEncoding)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify the input file current encoding."));
     }
     if (!checkedOptions.IsSetToEncoding)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify the output file target encoding."));
     }
     if (checkedOptions.IsSetInputFile && WildcardCharacterHelper.IsContainsWildcard(checkedOptions.InputFile)
       && checkedOptions.IsSetOutputFile)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "output file path has been set, so can only set one input file."));
     }
     if (checkedOptions.IsSetDirectory && checkedOptions.IsSetOutputFile)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "output file path has been set, so can not set a input directory."));
     }
       }
 }
Пример #6
0
        private static EncodeCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              EncodeCommandLineOptions targetOptions = new EncodeCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              EncodeOptionType optionType = EncodeOptions.GetOptionType(arg);
              if (optionType == EncodeOptionType.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 EncodeOptionType.InputFile:
              targetOptions.IsSetInputFile = true;
              targetOptions.InputFile = commandLineOptions.Arguments[arg];
              break;
            case EncodeOptionType.OutputFile:
              targetOptions.IsSetOutputFile = true;
              targetOptions.OutputFile = commandLineOptions.Arguments[arg];
              break;
            case EncodeOptionType.FromEncoding:
              targetOptions.IsSetFromEncoding = true;
              if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII")
              {
                targetOptions.FromEncoding = Encoding.ASCII;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7")
              {
                targetOptions.FromEncoding = Encoding.UTF7;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8")
              {
                targetOptions.FromEncoding = Encoding.UTF8;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE")
              {
                targetOptions.FromEncoding = Encoding.Unicode;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32")
              {
                targetOptions.FromEncoding = Encoding.UTF32;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE")
              {
                targetOptions.FromEncoding = 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 EncodeOptionType.ToEncoding:
              targetOptions.IsSetToEncoding = true;
              if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"ASCII")
              {
                targetOptions.ToEncoding = Encoding.ASCII;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF7")
              {
                targetOptions.ToEncoding = Encoding.UTF7;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF8")
              {
                targetOptions.ToEncoding = Encoding.UTF8;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UNICODE")
              {
                targetOptions.ToEncoding = Encoding.Unicode;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"UTF32")
              {
                targetOptions.ToEncoding = Encoding.UTF32;
              }
              else if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"BIGENDIANUNICODE")
              {
                targetOptions.ToEncoding = 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 EncodeOptionType.Directory:
              targetOptions.IsSetDirectory = true;
              targetOptions.Directory = commandLineOptions.Arguments[arg];
              break;
            case EncodeOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case EncodeOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case EncodeOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              if (commandLineOptions.Parameters.Count > 0)
              {
            if (!targetOptions.IsSetInputFile)
            {
              targetOptions.IsSetInputFile = true;
              targetOptions.InputFile = commandLineOptions.Parameters.First();
            }
              }

              return targetOptions;
        }