argparse documentation Help

Restrict allowed values

In some cases an argument can receive one of the limited set of values. This can be achieved by adding .AllowedValues!() to UDA:

import argparse; struct T { @(NamedArgument.AllowedValues!(["apple","pear","banana"])) string fruit; } T t; assert(CLI!T.parseArgs(t, ["--fruit", "apple"])); assert(t == T("apple")); // "kiwi" is not allowed assert(!CLI!T.parseArgs(t, ["--fruit", "kiwi"]));

For the value that is not in the allowed list, this error will be printed:

Allowed values error
Last modified: 07 August 2024