Example #1
0
        public override void Execute()
        {
            base.Execute();

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

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

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

            Terminate();
        }
Example #2
0
 private static void CheckOptions(SelectCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (!checkedOptions.IsSetDirectory || string.IsNullOrEmpty(checkedOptions.Directory))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a directory."));
         }
         if (!checkedOptions.IsSetExtension || string.IsNullOrEmpty(checkedOptions.Extension))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a file extension type."));
         }
         if (checkedOptions.IsSetOutput && string.IsNullOrEmpty(checkedOptions.Output))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "bad output directory."));
         }
         if (checkedOptions.IsSetOutput && !Directory.Exists(checkedOptions.Output))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "no such output directory."));
         }
     }
 }
Example #3
0
    public override void Execute()
    {
      base.Execute();

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

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

      Terminate();
    }
Example #4
0
 private static void CheckOptions(SelectCommandLineOptions checkedOptions)
 {
   if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
   {
     if (!checkedOptions.IsSetDirectory || string.IsNullOrEmpty(checkedOptions.Directory))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "must specify a directory."));
     }
     if (!checkedOptions.IsSetExtension || string.IsNullOrEmpty(checkedOptions.Extension))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "must specify a file extension type."));
     }
     if (checkedOptions.IsSetOutput && string.IsNullOrEmpty(checkedOptions.Output))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "bad output directory."));
     }
     if (checkedOptions.IsSetOutput && !Directory.Exists(checkedOptions.Output))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "no such output directory."));
     }
   }
 }
Example #5
0
    private static SelectCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
    {
      if (commandLineOptions == null)
        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
          "Option used in invalid context -- {0}", "must specify a option."));

      SelectCommandLineOptions targetOptions = new SelectCommandLineOptions();

      if (commandLineOptions.Arguments.Count >= 0)
      {
        foreach (var arg in commandLineOptions.Arguments.Keys)
        {
          SelectOptionType optionType = SelectOptions.GetOptionType(arg);
          if (optionType == SelectOptionType.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 SelectOptionType.Directory:
              targetOptions.IsSetDirectory = true;
              targetOptions.Directory = commandLineOptions.Arguments[arg];
              break;
            case SelectOptionType.Recursive:
              targetOptions.IsSetRecursive = true;
              break;
            case SelectOptionType.Extension:
              targetOptions.IsSetExtension = true;
              targetOptions.Extension = commandLineOptions.Arguments[arg];
              break;
            case SelectOptionType.Output:
              targetOptions.IsSetOutput = true;
              targetOptions.Output = commandLineOptions.Arguments[arg];
              break;
            case SelectOptionType.Copy:
              targetOptions.IsSetCopy = true;
              break;
            case SelectOptionType.Move:
              targetOptions.IsSetMove = true;
              break;
            case SelectOptionType.KeepDepth:
              targetOptions.IsSetKeepDepth = true;
              int depth = 0;
              if (!int.TryParse(commandLineOptions.Arguments[arg], out depth))
              {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                  "Option used in invalid context -- {0}", "invalid depth."));
              }
              if (depth <= 0 || depth > 10)
              {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                  "Option used in invalid context -- {0}", "invalid depth [1, 9]."));
              }
              targetOptions.KeepDepth = depth;
              break;
            case SelectOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case SelectOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
          }
        }
      }

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

      if (targetOptions.IsSetOutput && !targetOptions.IsSetMove)
      {
        targetOptions.IsSetCopy = true;
      }

      return targetOptions;
    }
Example #6
0
        private static SelectCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            SelectCommandLineOptions targetOptions = new SelectCommandLineOptions();

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

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

                    case SelectOptionType.Extension:
                        targetOptions.IsSetExtension = true;
                        targetOptions.Extension      = commandLineOptions.Arguments[arg];
                        break;

                    case SelectOptionType.Output:
                        targetOptions.IsSetOutput = true;
                        targetOptions.Output      = commandLineOptions.Arguments[arg];
                        break;

                    case SelectOptionType.Copy:
                        targetOptions.IsSetCopy = true;
                        break;

                    case SelectOptionType.Move:
                        targetOptions.IsSetMove = true;
                        break;

                    case SelectOptionType.KeepDepth:
                        targetOptions.IsSetKeepDepth = true;
                        int depth = 0;
                        if (!int.TryParse(commandLineOptions.Arguments[arg], out depth))
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid depth."));
                        }
                        if (depth <= 0 || depth > 10)
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid depth [1, 9]."));
                        }
                        targetOptions.KeepDepth = depth;
                        break;

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

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

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

            if (targetOptions.IsSetOutput && !targetOptions.IsSetMove)
            {
                targetOptions.IsSetCopy = true;
            }

            return(targetOptions);
        }