Пример #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();
        }
Пример #2
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);
        }