コード例 #1
0
        public override void Execute()
        {
            base.Execute();

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

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

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

            Terminate();
        }
コード例 #2
0
        private static void CheckOptions(ReplaceCommandLineOptions checkedOptions)
        {
            if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
            {
                if (checkedOptions.IsSetInputDirectory)
                {
                    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."));
                    }
                    if (checkedOptions.IsSetOutputFile)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "donot support output file path when specify the input directory."));
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(checkedOptions.InputFile))
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "must specify a input 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."));
                    }
                }

                if (string.IsNullOrEmpty(checkedOptions.FromText))
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "must specify a from string."));
                }
                if (string.IsNullOrEmpty(checkedOptions.ToText))
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "must specify a to string."));
                }
            }
        }
コード例 #3
0
    public override void Execute()
    {
      base.Execute();

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

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

      Terminate();
    }
コード例 #4
0
        private static ReplaceCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            ReplaceCommandLineOptions targetOptions = new ReplaceCommandLineOptions();

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

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

                    case ReplaceOptionType.FromText:
                        targetOptions.FromText = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.ToText:
                        targetOptions.ToText = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.InputDirectory:
                        targetOptions.IsSetInputDirectory = true;
                        targetOptions.InputDirectory      = commandLineOptions.Arguments[arg];
                        break;

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

                    case ReplaceOptionType.Extension:
                        targetOptions.Extensions.AddRange(
                            commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
                        break;

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

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

            if (commandLineOptions.Parameters.Count > 0)
            {
                if (string.IsNullOrEmpty(targetOptions.InputFile))
                {
                    targetOptions.InputFile = commandLineOptions.Parameters.First();
                }
            }

            return(targetOptions);
        }
コード例 #5
0
    private static void CheckOptions(ReplaceCommandLineOptions checkedOptions)
    {
      if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
      {
        if (checkedOptions.IsSetInputDirectory)
        {
          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."));
          }
          if (checkedOptions.IsSetOutputFile)
          {
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "donot support output file path when specify the input directory."));
          }
        }
        else
        {
          if (string.IsNullOrEmpty(checkedOptions.InputFile))
          {
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a input 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."));
          }
        }

        if (string.IsNullOrEmpty(checkedOptions.FromText))
        {
          throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "must specify a from string."));
        }
        if (string.IsNullOrEmpty(checkedOptions.ToText))
        {
          throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
            "Option used in invalid context -- {0}", "must specify a to string."));
        }
      }
    }
コード例 #6
0
    private static ReplaceCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
    {
      if (commandLineOptions == null)
        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
          "Option used in invalid context -- {0}", "must specify a option."));

      ReplaceCommandLineOptions targetOptions = new ReplaceCommandLineOptions();

      if (commandLineOptions.Arguments.Count >= 0)
      {
        foreach (var arg in commandLineOptions.Arguments.Keys)
        {
          ReplaceOptionType optionType = ReplaceOptions.GetOptionType(arg);
          if (optionType == ReplaceOptionType.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 ReplaceOptionType.InputFile:
              targetOptions.InputFile = commandLineOptions.Arguments[arg];
              break;
            case ReplaceOptionType.OutputFile:
              targetOptions.IsSetOutputFile = true;
              targetOptions.OutputFile = commandLineOptions.Arguments[arg];
              break;
            case ReplaceOptionType.FromText:
              targetOptions.FromText = commandLineOptions.Arguments[arg];
              break;
            case ReplaceOptionType.ToText:
              targetOptions.ToText = commandLineOptions.Arguments[arg];
              break;
            case ReplaceOptionType.InputDirectory:
              targetOptions.IsSetInputDirectory = true;
              targetOptions.InputDirectory = commandLineOptions.Arguments[arg];
              break;
            case ReplaceOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case ReplaceOptionType.Extension:
              targetOptions.Extensions.AddRange(
                commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
              break;
            case ReplaceOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case ReplaceOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
          }
        }
      }

      if (commandLineOptions.Parameters.Count > 0)
      {
        if (string.IsNullOrEmpty(targetOptions.InputFile))
        {
          targetOptions.InputFile = commandLineOptions.Parameters.First();
        }
      }

      return targetOptions;
    }