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

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

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

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

            Terminate();
        }
Пример #2
0
 private static void CheckOptions(CatCommandLineOptions 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."));
         }
     }
 }
Пример #3
0
        private static CatCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            CatCommandLineOptions targetOptions = new CatCommandLineOptions();

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

                    case CatOptionType.LineNumber:
                        targetOptions.IsSetLineNumber = true;
                        break;

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

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

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

            return(targetOptions);
        }
Пример #4
0
        public override void Execute()
        {
            base.Execute();

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

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

              Terminate();
        }
Пример #5
0
 private static void CheckOptions(CatCommandLineOptions 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."));
     }
       }
 }
Пример #6
0
        private static CatCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
              "Option used in invalid context -- {0}", "must specify a option."));

              CatCommandLineOptions targetOptions = new CatCommandLineOptions();

              if (commandLineOptions.Arguments.Count >= 0)
              {
            foreach (var arg in commandLineOptions.Arguments.Keys)
            {
              CatOptionType optionType = CatOptions.GetOptionType(arg);
              if (optionType == CatOptionType.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 CatOptionType.InputFile:
              targetOptions.InputFile = commandLineOptions.Arguments[arg];
              break;
            case CatOptionType.LineNumber:
              targetOptions.IsSetLineNumber = true;
              break;
            case CatOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case CatOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
              }
            }
              }

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

              return targetOptions;
        }