argparse documentation Help

Named arguments

Named arguments (they are also called as flags or options) have one or more names. Each name can be short, long or both. They can be declared using NamedArgument UDA which has the following parameters (see reference for details):

NamedArgument(string[] names...) NamedArgument(string[] shortNames, string[] longNames)

Name

Type

Optional/


Required

Description

name

string[]...

optional

Name(s) of this argument.

shortNames

string[]

required

Short name(s) of this argument.

longNames

string[]

required

Long name(s) of this argument.

Example:

import argparse; struct Params { // If name is not provided then member name is used: "--greeting" @NamedArgument string greeting; // If member name is single character then it becomes a short name: "-a" @NamedArgument string a; // Argument with multiple names: "--name", "--first-name", "-n" // Note that single character becomes a short name @NamedArgument(["name", "first-name", "n"]) string name; // Another way to specify multiple names: "--family", "--last-name" @NamedArgument("family", "last-name") string family; // Explicitly providing short and long names: "-m","-middle", "--middle", "--middle-name" @NamedArgument(["m","middle"], ["middle", "middle-name"]) string middle; }

Short names

  • Short names in command line are those that start with short name prefix which is a single dash - by default (see Config.shortNamePrefix for customization).

If short names are not explicitly passed to NamedArgument UDA then all single-character names are considered short names (see reference for details).

The following usages of the argument short name in the command line are equivalent:

  • -name John

  • -name=John

  • -n John

  • -n=John

Additionally, for single-character short names the following is supported:

Long names

Long names in command line are those that start with long name prefix which is double dash -- by default (see Config.longNamePrefix for customization).

If long names are not explicitly passed to NamedArgument UDA then all multi-character names are considered long names (see reference for details).

The following usages of the argument long names in the command line are equivalent:

  • --name John

  • --name=John

  • --n John

  • --n=John

Last modified: 06 May 2025