Skip to content

Parser

clap.Parser

A base class for static type checking.

Classes decorated with @clap.command will have a parse_args method injected at runtime. Inheriting from Parser provides this method signature to static type checkers, avoiding errors and enabling autocompletion in editors.

This class is not strictly required for functionality.

Example:

import clap

@clap.command
class Cli(clap.Parser):
    ...

args = Cli.parse_args()

Methods:

parse_args classmethod

parse_args(args: Optional[Sequence[str]] = None) -> Self

Parse from sys.argv, exit on error.

Source code in clap/api.py
44
45
46
47
@classmethod
def parse_args(cls: type[Self], args: Optional[Sequence[str]] = None) -> Self:
    """Parse from [`sys.argv`][], exit on error."""
    ...