public static HeadOptionType GetOptionType(string option) { HeadOptionType optionType = HeadOptionType.None; foreach (var pair in Options) { foreach (var item in pair.Value) { if (item == option) { optionType = pair.Key; break; } } } return(optionType); }
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); }