in plugin-dotnet-agent/src/main/csharp/TeamCity.Dotnet.TestSuppressor/TeamCity.Dotnet.TestSuppressor/Infrastructure/CommandLine/Parsing/CommandLineParser.cs [22:90]
public IConfigurationParsingResult Parse(IEnumerable<string> args)
{
var commandPath = new List<string> { _commandType.Name };
var mappingsResult = new Dictionary<string, string>
{
{ Key(commandPath, nameof(Command.IsActive)), True } // RootCommand:IsActive --> true – always true for command
};
var unknownArguments = new List<string>();
var arguments = new Queue<string>(args);
var commandType = _commandType;
var prevKey = string.Empty;
// we have 5 possibilities for every single argument:
// - it's a command
// - it's an option required value
// - it's a value for option defined in previous argument
// - it's an option flag
// - it's an unknown argument
while (arguments.TryDequeue(out var argument))
{
var properties = commandType.GetProperties();
// if prev argument was option required value – current argument is value
if (!string.IsNullOrWhiteSpace(prevKey) && !IsArgumentOption(properties, argument) && !IsArgumentCommand(properties, argument))
{
mappingsResult.Add(prevKey, argument);
prevKey = string.Empty; // reset key and value
continue;
}
var (isOption, key) = TryParseOption(properties, argument, commandPath, mappingsResult);
if (isOption)
{
if (key != string.Empty)
{
prevKey = key;
}
continue;
}
var (isCommand, cmdType) = TryParseCommand(properties, argument, mappingsResult, commandPath);
if (isCommand)
{
if (cmdType != null)
{
commandType = cmdType;
}
continue;
}
// unknown argument
unknownArguments.Add(argument);
}
if (commandPath.Count <= 1)
{
return new CommandLineParsingResult(mappingsResult, unknownArguments);
}
// if there is a several commands in command path
// put verbosity value to the root level command since it's valid only for root level command
if (mappingsResult.TryGetValue(Key(commandPath, nameof(Command.Verbosity)), out var verbosity))
{
mappingsResult.Add(Key(new[] { _commandType.Name }, nameof(Command.Verbosity)), verbosity);
}
return new CommandLineParsingResult(mappingsResult, unknownArguments);
}