Skip to content

Introduce ParseResult.GetValue<T>(string name) #2052

Closed
@adamsitnik

Description

@adamsitnik
  • Providing name should be mandatory when creating Argument<T> and Option<T> #2051 needs to be implemented first. The method should work only for the names (no short aliases). So if user defines Option<bool>(name: "help", aliases: new [] { "--help", "-h", "-?" }) the lookup should match only help.
  • The users might define a parse tree where there are multiple arguments/options with the same name. But conflicts are not allowed on the Command level. The method should handle that properly.
  • The method needs to handle default values properly (value not specified in args, default value factory used to create it).
  • The lookup needs to be fast, so it should be backed by a Dictionary. No need to use a concurrent version of dictionary, as the creation will be single-threaded.

One way of implementing it (just an idea) is creating a new dedicated class (not a Value Tuple because it's a struct and JIT compiles a dedicated version of generic collections for every value type and we don't want that as it's a performance hit for startup scenarios).
The class should store string name and CommandResult parent as fields, implement IEqutable and override GetHashCode and be internal and sealed. Instances of this class could be keys of the mentioned dictionary and they should be added to the dictionary at the same time when ArgumentResult instances are being added to SymbolResultTree (the values of the dictionary should be instances of ArgumentResult).

_symbolResultTree.Add(argument, argumentResult);

Finding all usages of SymbolResultTree.*Add* might be non-trivial, as the type derives from Dictionary. This can be changed, it's internal type and it was just a minor perf optimization.

When ParseResult.GetValue<T>(string name) is used, it should use the parsed CommandResult to find the value of given name in the context of parsed Command:

public CommandResult CommandResult { get; }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions