import argparse
import io


class CustomArgParser(argparse.ArgumentParser):
    def __init__(
            self,
            *args, **kwargs,
    ):
        super().__init__(*args, **kwargs)
        self.help_message = None

    def print_help(self, file=None):
        # Store the help message in a buffer instead of printing
        if file is None:
            help_buffer = io.StringIO()
            super().print_help(file=help_buffer)
            self.help_message = help_buffer.getvalue()
            help_buffer.close()
        else:
            super().print_help(file=file)

    def parse_args(self, args=None, namespace=None):
        # Check for --help manually to avoid stdout output
        if '--help' in args:
            self.print_help()
            raise RuntimeError("Help requested")
        return super().parse_args(args, namespace)

    def exit(self, status=0, message=None):
        raise RuntimeError(message)

    def error(self, message):
        raise RuntimeError(f"Error: {message}")

