Skip to content

Actions

clap.ArgAction

Bases: StrEnum

Classes:

  • Help

    When encountered, display help information.

  • HelpLong

    When encountered, display long help information.

  • HelpShort

    When encountered, display short help information.

  • Version

    When encountered, display version information.

Attributes:

  • Append

    When encountered, store the associated value(s) in a list.

  • Count

    When encountered, increment an int counter starting from 0.

  • Extend

    When encountered, extend a list with the associated values.

  • Set

    When encountered, store the associated value(s).

  • SetFalse

    When encountered, act as if False was encountered on the command-line.

  • SetTrue

    When encountered, act as if True was encountered on the command-line.

Append class-attribute instance-attribute

Append = 'append'

When encountered, store the associated value(s) in a list.

Example:

import clap
from clap import ArgAction, long

@clap.command
class Cli(clap.Parser):
    files: list[str] = arg(long, action=ArgAction.Append)

args = Cli.parse_args(["--files", "a.txt", "--files", "b.txt"])
assert args.files == ["a.txt", "b.txt"]

args = Cli.parse_args([])
assert args.files == []

Count class-attribute instance-attribute

Count = 'count'

When encountered, increment an int counter starting from 0.

Example:

import clap
from clap import ArgAction, short

@clap.command
class Cli(clap.Parser):
    verbose: int = arg(short, action=ArgAction.Count)

args = Cli.parse_args(["-vvv"])
assert args.verbose == 3

args = Cli.parse_args([])
assert args.verbose == 0

Extend class-attribute instance-attribute

Extend = 'extend'

When encountered, extend a list with the associated values.

Example:

import clap
from clap import ArgAction, long

@clap.command
class Cli(clap.Parser):
    items: list[str] = arg(long, action=ArgAction.Extend, num_args='+')

args = Cli.parse_args(["--items", "a", "b", "--items", "c", "d"])
assert args.items == ["a", "b", "c", "d"]

args = Cli.parse_args([])
assert args.items == []

Set class-attribute instance-attribute

Set = 'store'

When encountered, store the associated value(s).

Example:

import clap
from clap import ArgAction, long

@clap.command
class Cli(clap.Parser):
    output: str = arg(long, action=ArgAction.Set)

args = Cli.parse_args(["--output", "file.txt"])
assert args.output == "file.txt"

SetFalse class-attribute instance-attribute

SetFalse = 'store_false'

When encountered, act as if False was encountered on the command-line.

Example:

import clap
from clap import ArgAction, long

@clap.command
class Cli(clap.Parser):
    quiet: bool = arg(long, action=ArgAction.SetFalse)

args = Cli.parse_args(["--quiet"])
assert args.quiet == False

args = Cli.parse_args([])
assert args.quiet == True

SetTrue class-attribute instance-attribute

SetTrue = 'store_true'

When encountered, act as if True was encountered on the command-line.

Example:

import clap
from clap import ArgAction, long

@clap.command
class Cli(clap.Parser):
    verbose: bool = arg(long, action=ArgAction.SetTrue)

args = Cli.parse_args(["--verbose"])
assert args.verbose == True

args = Cli.parse_args([])
assert args.verbose == False

Help

Help(option_strings, dest, **kwargs)

Bases: Action

When encountered, display help information.

Depending on the flag, long_help may be shown.

Source code in clap/models.py
168
169
def __init__(self, option_strings, dest, **kwargs):
    super().__init__(option_strings, dest, nargs=0)

HelpLong

HelpLong(option_strings, dest, **kwargs)

Bases: Action

When encountered, display long help information.

Source code in clap/models.py
193
194
def __init__(self, option_strings, dest, **kwargs):
    super().__init__(option_strings, dest, nargs=0)

HelpShort

HelpShort(option_strings, dest, **kwargs)

Bases: Action

When encountered, display short help information.

Source code in clap/models.py
182
183
def __init__(self, option_strings, dest, **kwargs):
    super().__init__(option_strings, dest, nargs=0)

Version

Version(option_strings, dest, **kwargs)

Bases: Action

When encountered, display version information.

Depending on the flag, long_version may be shown.

Source code in clap/models.py
151
152
def __init__(self, option_strings, dest, **kwargs):
    super().__init__(option_strings, dest, nargs=0)