Skip to content

argparse_main

Main CLI parser setup for the Merlin command-line interface.

This module defines the primary argument parser for the merlin CLI tool, including custom error handling and integration of all available subcommands.

HelpParser

Bases: ArgumentParser

This class overrides the error message of the argument parser to print the help message when an error happens.

Methods:

Name Description
error

Override the error message of the ArgumentParser class.

Source code in merlin/cli/argparse_main.py
class HelpParser(ArgumentParser):
    """
    This class overrides the error message of the argument parser to
    print the help message when an error happens.

    Methods:
        error: Override the error message of the `ArgumentParser` class.
    """

    def error(self, message: str):
        """
        Override the error message of the `ArgumentParser` class.

        Args:
            message: The error message to log.
        """
        sys.stderr.write(f"error: {message}\n")
        self.print_help()
        sys.exit(2)

error(message)

Override the error message of the ArgumentParser class.

Parameters:

Name Type Description Default
message str

The error message to log.

required
Source code in merlin/cli/argparse_main.py
def error(self, message: str):
    """
    Override the error message of the `ArgumentParser` class.

    Args:
        message: The error message to log.
    """
    sys.stderr.write(f"error: {message}\n")
    self.print_help()
    sys.exit(2)

build_main_parser()

Set up the command-line argument parser for the Merlin package.

Returns:

Type Description
ArgumentParser

An ArgumentParser object with every parser defined in Merlin's codebase.

Source code in merlin/cli/argparse_main.py
def build_main_parser() -> ArgumentParser:
    """
    Set up the command-line argument parser for the Merlin package.

    Returns:
        An `ArgumentParser` object with every parser defined in Merlin's codebase.
    """
    parser = HelpParser(
        prog="merlin",
        description=banner_small,
        formatter_class=RawDescriptionHelpFormatter,
        epilog="See merlin <command> --help for more info",
    )
    parser.add_argument("-v", "--version", action="version", version=VERSION)
    parser.add_argument(
        "-lvl",
        "--level",
        type=str,
        default=DEFAULT_LOG_LEVEL,
        help="Set log level: DEBUG, INFO, WARNING, ERROR [Default: %(default)s]",
    )
    subparsers = parser.add_subparsers(dest="subparsers", required=True)

    for command in ALL_COMMANDS:
        command.add_parser(subparsers)

    return parser