Exemple #1
0
        public override void Execute()
        {
            base.Execute();

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

            tailOptions = ParseOptions(options);

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

            TailCommandLineOptions targetOptions = new TailCommandLineOptions();

            if (options.Arguments.Count > 0)
            {
                foreach (var arg in options.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        = options.Arguments[arg];
                        break;

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

                    case TailOptionType.OutputLines:
                        long outputLines = 0;
                        if (!long.TryParse(options.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(options.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 (options.Parameters.Count <= 0)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "must follow a file."));
                    }
                    if (options.Parameters.Count > 1)
                    {
                        throw new CommandLineException(string.Format(CultureInfo.CurrentCulture,
                                                                     "Option used in invalid context -- {0}", "can only follow one file."));
                    }

                    targetOptions.File = options.Parameters.First();
                }
                else
                {
                    if (options.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);
        }