Skip to content

Calling AddError in the DefaultValueFactory will cause exception when showing help #2640

@FMsrAtLapl

Description

@FMsrAtLapl

Calling AddError in the DefaultValueFactory will cause an exception when showing help
(provided the condition which triggers the error exists)

Help seemingly tries to create any default value, and in case it needs to call the AddError ( )-method
it tries to add to an internal structure which does not exist in "help-mode"

Probably it is not the best idea to add errors when creating default values
but then I took this example from the
Microsoft Tutorial

Option<FileInfo> fileOption = new("--file")
{
    Description = "An option whose argument is parsed as a FileInfo",
    Required = true,
    DefaultValueFactory = result =>
    {
        if (result.Tokens.Count == 0)
        {
            return new FileInfo("sampleQuotes.txt");

        }
        string filePath = result.Tokens.Single().Value;
        if (!File.Exists(filePath))
        {
            result.AddError("File does not exist");
            return null;
        }
        else
        {
            return new FileInfo(filePath);
        }
    }
};

Metadata

Metadata

Assignees

Labels

Area-APIbugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions