argparse documentation Help

Named arguments

Named arguments (they are also called as flags or options) have one or more name that can be separated into two categories:

Both cases are fully supported with one caveat: if a single-character argument is used with a double dash (e.g., --n) in command line, then it behaves the same as a multi-character argument.

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

  • --name John

  • --name=John

  • --n John

  • --n=John

  • -n John

  • -n=John

  • -nJohn - this works for single-character names only

Named arguments can be declared using NamedArgument UDA which has the following parameters:

#

Name

Type

Optional/


Required

Description

1

name

string or string[]

optional

Name(s) of this argument that can show up in command line.

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; }
Last modified: 07 August 2024