public override void Execute()
        {
            base.Execute();

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

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

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

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

            AddTextCommandLineOptions targetOptions = new AddTextCommandLineOptions();

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

                    case AddTextOptionType.Text:
                        targetOptions.Text = commandLineOptions.Arguments[arg];
                        break;

                    case AddTextOptionType.FromFile:
                        targetOptions.IsSetFromFile = true;
                        targetOptions.FromFile      = commandLineOptions.Arguments[arg];
                        break;

                    case AddTextOptionType.Top:
                        targetOptions.IsSetTop = true;
                        break;

                    case AddTextOptionType.Bottom:
                        targetOptions.IsSetBottom = true;
                        break;

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

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

            return(targetOptions);
        }
 private static void CheckOptions(AddTextCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
     {
         if (string.IsNullOrEmpty(checkedOptions.File))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a file."));
         }
         if (checkedOptions.IsSetFromFile)
         {
             if (string.IsNullOrEmpty(checkedOptions.FromFile))
             {
                 throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                              "Option used in invalid context -- {0}", "bad from file path."));
             }
         }
         else
         {
             if (string.IsNullOrEmpty(checkedOptions.Text))
             {
                 throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                              "Option used in invalid context -- {0}", "must specify the text string."));
             }
         }
         if (checkedOptions.IsSetFromFile && !string.IsNullOrEmpty(checkedOptions.Text))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "conflict options when specified text string."));
         }
         if (!checkedOptions.IsSetTop && !checkedOptions.IsSetBottom)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a text position option."));
         }
         if (checkedOptions.IsSetTop && checkedOptions.IsSetBottom)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "conflict options when specified text position."));
         }
     }
 }
Example #4
0
        public override void Execute()
        {
            base.Execute();

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

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

              Terminate();
        }
Example #5
0
 private static void CheckOptions(AddTextCommandLineOptions checkedOptions)
 {
     if (!checkedOptions.IsSetHelp && !checkedOptions.IsSetVersion)
       {
     if (string.IsNullOrEmpty(checkedOptions.File))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify a file."));
     }
     if (checkedOptions.IsSetFromFile)
     {
       if (string.IsNullOrEmpty(checkedOptions.FromFile))
       {
     throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
       "Option used in invalid context -- {0}", "bad from file path."));
       }
     }
     else
     {
       if (string.IsNullOrEmpty(checkedOptions.Text))
       {
     throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
       "Option used in invalid context -- {0}", "must specify the text string."));
       }
     }
     if (checkedOptions.IsSetFromFile && !string.IsNullOrEmpty(checkedOptions.Text))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "conflict options when specified text string."));
     }
     if (!checkedOptions.IsSetTop && !checkedOptions.IsSetBottom)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "must specify a text position option."));
     }
     if (checkedOptions.IsSetTop && checkedOptions.IsSetBottom)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
     "Option used in invalid context -- {0}", "conflict options when specified text position."));
     }
       }
 }
Example #6
0
        private static AddTextCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              AddTextCommandLineOptions targetOptions = new AddTextCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              AddTextOptionType optionType = AddTextOptions.GetOptionType(arg);
              if (optionType == AddTextOptionType.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 AddTextOptionType.File:
              targetOptions.File = commandLineOptions.Arguments[arg];
              break;
            case AddTextOptionType.Text:
              targetOptions.Text = commandLineOptions.Arguments[arg];
              break;
            case AddTextOptionType.FromFile:
              targetOptions.IsSetFromFile = true;
              targetOptions.FromFile = commandLineOptions.Arguments[arg];
              break;
            case AddTextOptionType.Top:
              targetOptions.IsSetTop = true;
              break;
            case AddTextOptionType.Bottom:
              targetOptions.IsSetBottom = true;
              break;
            case AddTextOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case AddTextOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

              return targetOptions;
        }