Skip to content

Parser

clap.Parser

A base class for static type checking.

Classes decorated with @clap.command will have a parse 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()

Methods:

  • parse

    Parse from the provided args or sys.argv, exit on error.

parse abstractmethod classmethod

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

Parse from the provided args or sys.argv, exit on error.

Source code in clap/api.py
56
57
58
59
@classmethod
@abstractmethod
def parse(cls: type[Self], args: Optional[Sequence[str]] = None) -> Self:
    """Parse from the provided `args` or [`sys.argv`][], exit on error."""