Esempio n. 1
0
        public override void Execute()
        {
            base.Execute();

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

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

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

            Terminate();
        }
Esempio n. 2
0
        private static SortCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            SortCommandLineOptions targetOptions = new SortCommandLineOptions();

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

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

                    case SortOptionType.ReverseOrder:
                        targetOptions.IsSetReverseOrder = true;
                        break;

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

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

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

            return(targetOptions);
        }
Esempio n. 3
0
 private static void CheckOptions(SortCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (string.IsNullOrEmpty(checkedOptions.InputFile))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a 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."));
         }
     }
 }
Esempio n. 4
0
    public override void Execute()
    {
      base.Execute();

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

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

      Terminate();
    }
Esempio n. 5
0
 private static void CheckOptions(SortCommandLineOptions checkedOptions)
 {
   if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
   {
     if (string.IsNullOrEmpty(checkedOptions.InputFile))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "must specify a 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."));
     }
   }
 }
Esempio n. 6
0
    private static SortCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
    {
      if (commandLineOptions == null)
        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
          "Option used in invalid context -- {0}", "must specify a option."));

      SortCommandLineOptions targetOptions = new SortCommandLineOptions();

      if (commandLineOptions.Arguments.Count >= 0)
      {
        foreach (var arg in commandLineOptions.Arguments.Keys)
        {
          SortOptionType optionType = SortOptions.GetOptionType(arg);
          if (optionType == SortOptionType.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 SortOptionType.InputFile:
              targetOptions.InputFile = commandLineOptions.Arguments[arg];
              break;
            case SortOptionType.OutputFile:
              targetOptions.IsSetOutputFile = true;
              targetOptions.OutputFile = commandLineOptions.Arguments[arg];
              break;
            case SortOptionType.ReverseOrder:
              targetOptions.IsSetReverseOrder = true;
              break;
            case SortOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case SortOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
          }
        }
      }

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

      return targetOptions;
    }