argparse documentation Help

Argument dependencies

Mutually exclusive arguments

Mutually exclusive arguments (i.e., those that can’t be used together) can be declared using MutuallyExclusive() UDA:

import argparse; struct T { @MutuallyExclusive() { string a; string b; } } T t; // One of them or no argument is allowed assert(CLI!T.parseArgs(t, ["-a","a"])); assert(CLI!T.parseArgs(t, ["-b","b"])); assert(CLI!T.parseArgs(t, [])); // Both arguments or no argument is not allowed assert(!CLI!T.parseArgs(t, ["-a","a","-b","b"]));

Set of mutually exclusive arguments can be marked as required in order to require exactly one of the arguments:

import argparse; struct T { @(MutuallyExclusive.Required) { string a; string b; } } T t; // Either argument is allowed assert(CLI!T.parseArgs(t, ["-a","a"])); assert(CLI!T.parseArgs(t, ["-b","b"])); // Both or no arguments are not allowed assert(!CLI!T.parseArgs(t, ["-a","a","-b","b"])); assert(!CLI!T.parseArgs(t, []));

Mutually required arguments

Mutually required arguments (i.e., those that require other arguments) can be declared using RequiredTogether() UDA:

import argparse; struct T { @RequiredTogether() { string a; string b; } } T t; // Both or no argument is allowed assert(CLI!T.parseArgs(t, ["-a","a","-b","b"])); assert(CLI!T.parseArgs(t, [])); // Only one argument is not allowed assert(!CLI!T.parseArgs(t, ["-a","a"])); assert(!CLI!T.parseArgs(t, ["-b","b"]));

Set of mutually required arguments can be marked as required in order to require all arguments:

import argparse; struct T { @(RequiredTogether.Required) { string a; string b; } } T t; // Both arguments are allowed assert(CLI!T.parseArgs(t, ["-a","a","-b","b"])); // Single argument or no argument is not allowed assert(!CLI!T.parseArgs(t, ["-a","a"])); assert(!CLI!T.parseArgs(t, ["-b","b"])); assert(!CLI!T.parseArgs(t, []));
Last modified: 09 November 2024