示例#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();
        }
示例#2
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);
        }