public override void Execute() { base.Execute(); List <string> singleOptionList = HeadOptions.GetSingleOptions(); CommandLineOptions cloptions = CommandLineParser.Parse(Arguments.ToArray <string>(), singleOptionList.ToArray()); options = ParseOptions(cloptions); CheckOptions(options); if (options.IsSetHelp) { RaiseCommandLineUsage(this, HeadOptions.Usage); } else if (options.IsSetVersion) { RaiseCommandLineUsage(this, Version); } else { StartHead(); } Terminate(); }
private static HeadCommandLineOptions ParseOptions(CommandLineOptions commandLineOptions) { if (commandLineOptions == null) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "must specify a option.")); } HeadCommandLineOptions targetOptions = new HeadCommandLineOptions(); if (commandLineOptions.Arguments.Count >= 0) { foreach (var arg in commandLineOptions.Arguments.Keys) { HeadOptionType optionType = HeadOptions.GetOptionType(arg); if (optionType == HeadOptionType.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 HeadOptionType.File: targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Arguments[arg]; break; case HeadOptionType.Number: 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 number.")); } if (outputLines <= 0) { throw new CommandLineException(string.Format(CultureInfo.CurrentCulture, "Option used in invalid context -- {0}", "invalid output lines number.")); } targetOptions.Number = outputLines; break; case HeadOptionType.Help: targetOptions.IsSetHelp = true; break; case HeadOptionType.Version: targetOptions.IsSetVersion = true; break; } } } if (commandLineOptions.Parameters.Count > 0) { if (!targetOptions.IsSetFile) { targetOptions.IsSetFile = true; targetOptions.File = commandLineOptions.Parameters.First(); } } return(targetOptions); }