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

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

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

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

            Terminate();
        }
Exemplo n.º 2
0
 private static void CheckOptions(ChecksumCommandLineOptions options)
 {
     if (!options.IsSetHelp && !options.IsSetVersion)
     {
         if (!options.IsSetAlgorithm || string.IsNullOrEmpty(options.Algorithm))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a algorithm."));
         }
         if (!options.IsSetFile && !options.IsSetText)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "must specify a file or a text."));
         }
         if (options.IsSetFile && options.IsSetText)
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "can only specify a file or a text."));
         }
         if (options.IsSetFile && string.IsNullOrEmpty(options.File))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "bad file path."));
         }
         if (options.IsSetText && string.IsNullOrEmpty(options.Text))
         {
             throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                          "Option used in invalid context -- {0}", "bad text format."));
         }
     }
 }
Exemplo n.º 3
0
    public override void Execute()
    {
      base.Execute();

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

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

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

            ChecksumCommandLineOptions targetOptions = new ChecksumCommandLineOptions();

            if (commandLineOptions.Arguments.Count >= 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    ChecksumOptionType optionType = ChecksumOptions.GetOptionType(arg);
                    if (optionType == ChecksumOptionType.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 ChecksumOptionType.Algorithm:
                        targetOptions.IsSetAlgorithm = true;
                        if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC32" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC64" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"MD5" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA1" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA256" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA384" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA512" ||
                            commandLineOptions.Arguments[arg].ToUpperInvariant() == @"RIPEMD160"
                            )
                        {
                            targetOptions.Algorithm = commandLineOptions.Arguments[arg];
                        }
                        else
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid algorithm, support CRC32, CRC64, MD5, SHA1, SHA256, SHA384, SHA512, RIPEMD160."));
                        }
                        break;

                    case ChecksumOptionType.File:
                        targetOptions.IsSetFile = true;
                        targetOptions.File      = commandLineOptions.Arguments[arg];
                        break;

                    case ChecksumOptionType.Text:
                        targetOptions.IsSetText = true;
                        targetOptions.Text      = commandLineOptions.Arguments[arg];
                        break;

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

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

            return(targetOptions);
        }
Exemplo n.º 5
0
 private static void CheckOptions(ChecksumCommandLineOptions options)
 {
   if (!options.IsSetHelp && !options.IsSetVersion)
   {
     if (!options.IsSetAlgorithm || string.IsNullOrEmpty(options.Algorithm))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "must specify a algorithm."));
     }
     if (!options.IsSetFile && !options.IsSetText)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "must specify a file or a text."));
     }
     if (options.IsSetFile && options.IsSetText)
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "can only specify a file or a text."));
     }
     if (options.IsSetFile && string.IsNullOrEmpty(options.File))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "bad file path."));
     }
     if (options.IsSetText && string.IsNullOrEmpty(options.Text))
     {
       throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
         "Option used in invalid context -- {0}", "bad text format."));
     }
   }
 }
Exemplo n.º 6
0
    private static ChecksumCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
    {
      if (commandLineOptions == null)
        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
          "Option used in invalid context -- {0}", "must specify a option."));

      ChecksumCommandLineOptions targetOptions = new ChecksumCommandLineOptions();

      if (commandLineOptions.Arguments.Count >= 0)
      {
        foreach (var arg in commandLineOptions.Arguments.Keys)
        {
          ChecksumOptionType optionType = ChecksumOptions.GetOptionType(arg);
          if (optionType == ChecksumOptionType.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 ChecksumOptionType.Algorithm:
              targetOptions.IsSetAlgorithm = true;
              if (commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC32"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"CRC64"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"MD5"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA1"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA256"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA384"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"SHA512"
                || commandLineOptions.Arguments[arg].ToUpperInvariant() == @"RIPEMD160"
                )
              {
                targetOptions.Algorithm = commandLineOptions.Arguments[arg];
              }
              else
              {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                  "Option used in invalid context -- {0}", "invalid algorithm, support CRC32, CRC64, MD5, SHA1, SHA256, SHA384, SHA512, RIPEMD160."));
              }
              break;
            case ChecksumOptionType.File:
              targetOptions.IsSetFile = true;
              targetOptions.File = commandLineOptions.Arguments[arg];
              break;
            case ChecksumOptionType.Text:
              targetOptions.IsSetText = true;
              targetOptions.Text = commandLineOptions.Arguments[arg];
              break;
            case ChecksumOptionType.Help:
              targetOptions.IsSetHelp = true;
              break;
            case ChecksumOptionType.Version:
              targetOptions.IsSetVersion = true;
              break;
          }
        }
      }

      return targetOptions;
    }