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

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

            options = ParseOptions(cloptions);

            if (options.IsSetHelp)
            {
                RaiseCommandLineUsage(this, TailOptions.Usage);
                Terminate();
            }
            else if (options.IsSetVersion)
            {
                RaiseCommandLineUsage(this, Version);
                Terminate();
            }
            else
            {
                StartWatch();
            }
        }
Esempio n. 2
0
        private static TailCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions)
        {
            if (commandLineOptions == null)
            {
                throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                             "Option used in invalid context -- {0}", "must specify a option."));
            }

            TailCommandLineOptions targetOptions = new TailCommandLineOptions();

            if (commandLineOptions.Arguments.Count > 0)
            {
                foreach (var arg in commandLineOptions.Arguments.Keys)
                {
                    TailOptionType optionType = TailOptions.GetOptionType(arg);
                    if (optionType == TailOptionType.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 TailOptionType.Retry:
                        targetOptions.IsSetRetry = true;
                        break;

                    case TailOptionType.Follow:
                        targetOptions.IsSetFollow = true;
                        targetOptions.File        = commandLineOptions.Arguments[arg];
                        break;

                    case TailOptionType.FollowRetry:
                        targetOptions.IsSetFollow = true;
                        targetOptions.IsSetRetry  = true;
                        targetOptions.File        = commandLineOptions.Arguments[arg];
                        break;

                    case TailOptionType.OutputLines:
                        long outputLines = 0;
                        if (!long.TryParse(commandLineOptions.Arguments[arg], out outputLines))
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid output lines option value."));
                        }
                        if (outputLines <= 0)
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid output lines option value."));
                        }
                        targetOptions.OutputLines = outputLines;
                        break;

                    case TailOptionType.SleepInterval:
                        long sleepInterval = 0;
                        if (!long.TryParse(commandLineOptions.Arguments[arg], out sleepInterval))
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid sleep interval option value."));
                        }
                        if (sleepInterval <= 0)
                        {
                            throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                         "Option used in invalid context -- {0}", "invalid sleep interval option value."));
                        }
                        targetOptions.SleepInterval = sleepInterval;
                        break;

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

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

            if (!targetOptions.IsSetHelp && !targetOptions.IsSetVersion)
            {
                if (string.IsNullOrEmpty(targetOptions.File))
                {
                    if (commandLineOptions.Parameters.Count <= 0)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "must follow a file."));
                    }
                    if (commandLineOptions.Parameters.Count > 1)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "can only follow one file."));
                    }

                    targetOptions.File = commandLineOptions.Parameters.First();
                }
                else
                {
                    if (commandLineOptions.Parameters.Count > 0)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "can only follow one file."));
                    }
                }

                if (targetOptions.IsSetRetry && !targetOptions.IsSetFollow)
                {
                    throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                 "Option used in invalid context -- {0}", "keep trying to open a file should follow a file name explicitly."));
                }
            }

            return(targetOptions);
        }