private ParseOptionResult ParseOption( bool isLongOption, CommandLineApplication command, string[] args, ref int index, out CommandOption option) { option = null; ParseOptionResult result = ParseOptionResult.Succeeded; var arg = args[index]; int optionPrefixLength = isLongOption ? 2 : 1; string[] optionComponents = arg.Substring(optionPrefixLength).Split(new[] { ':', '=' }, 2); string optionName = optionComponents[0]; if (isLongOption) { option = command.Options.SingleOrDefault( opt => string.Equals(opt.LongName, optionName, StringComparison.Ordinal)); } else { option = command.Options.SingleOrDefault( opt => string.Equals(opt.ShortName, optionName, StringComparison.Ordinal)); if (option == null) { option = command.Options.SingleOrDefault( opt => string.Equals(opt.SymbolName, optionName, StringComparison.Ordinal)); } } if (option == null) { if (isLongOption && string.IsNullOrEmpty(optionName) && !command._throwOnUnexpectedArg && AllowArgumentSeparator) { // a stand-alone "--" is the argument separator, so skip it and // handle the rest of the args as unexpected args index++; } HandleUnexpectedArg(command, args, index, argTypeName: "option"); result = ParseOptionResult.UnexpectedArgs; } else if (command.OptionHelp == option) { result = ParseOptionResult.ShowHelp; } else if (command.OptionVersion == option) { result = ParseOptionResult.ShowVersion; } else { if (optionComponents.Length == 2) { if (!option.TryParse(optionComponents[1])) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{optionComponents[1]}' for option '{optionName}'"); } } else { if (option.OptionType == CommandOptionType.NoValue || option.OptionType == CommandOptionType.BoolValue) { // No value is needed for this option option.TryParse(null); } else { index++; arg = args[index]; if (!option.TryParse(arg)) { command.ShowHint(); throw new CommandParsingException(command, $"Unexpected value '{arg}' for option '{optionName}'"); } } } } return(result); }
public CommandParsingException(CommandLineApplication command, string message) : base(message) { Command = command; }