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

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

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

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

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

            ReplaceCommandLineOptions targetOptions = new ReplaceCommandLineOptions();

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

                    case ReplaceOptionType.OutputFile:
                        targetOptions.IsSetOutputFile = true;
                        targetOptions.OutputFile      = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.FromText:
                        targetOptions.FromText = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.ToText:
                        targetOptions.ToText = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.InputDirectory:
                        targetOptions.IsSetInputDirectory = true;
                        targetOptions.InputDirectory      = commandLineOptions.Arguments[arg];
                        break;

                    case ReplaceOptionType.Recursive:
                        targetOptions.IsSetRecursive = true;
                        break;

                    case ReplaceOptionType.Extension:
                        targetOptions.Extensions.AddRange(
                            commandLineOptions.Arguments[arg].Trim().Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList());
                        break;

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

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

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

            return(targetOptions);
        }