def main()

in src/rpdk/core/cli.py [0:0]


def main(args_in=None):  # pylint: disable=too-many-statements
    """The entry point for the CLI."""
    log = None
    try:
        # see docstring of this file
        parser = argparse.ArgumentParser(description=__doc__)
        # the default command just prints the help message
        # subparsers should set their own default commands
        # also need to set verbose here because now it only gets set if a
        # subcommand is run (which is okay, the help doesn't need it)

        def no_command(args):
            if args.version:
                print("cfn", __version__)
            else:
                parser.print_help()

        parser.set_defaults(command=no_command, verbose=0)
        parser.add_argument(
            "--version",
            action="store_true",
            help="Show the executable version and exit.",
        )

        base_subparser = argparse.ArgumentParser(add_help=False)
        # shared arguments
        base_subparser.add_argument(
            "-v",
            "--verbose",
            action="count",
            default=0,
            help="Increase the output verbosity. Can be specified multiple times.",
        )
        parents = [base_subparser]

        subparsers = parser.add_subparsers(dest="subparser_name")
        init_setup_subparser(subparsers, parents)
        validate_setup_subparser(subparsers, parents)
        submit_setup_subparser(subparsers, parents)
        generate_setup_subparser(subparsers, parents)
        test_setup_subparser(subparsers, parents)
        invoke_setup_subparser(subparsers, parents)
        unittest_patch_setup_subparser(subparsers, parents)
        build_image_setup_subparser(subparsers, parents)
        args = parser.parse_args(args=args_in)

        setup_logging(args.verbose)

        log = logging.getLogger(__name__)
        log.debug("Logging set up successfully")
        log.debug("Running %s: %s", args.subparser_name, args)

        with colorama_text():
            args.command(args)

        log.debug("Finished %s", args.subparser_name)
    except SysExitRecommendedError as e:
        # This is to unify exit messages, and avoid throwing SystemExit in
        # library code, which is hard to catch for consumers. (There are still
        # some cases where it makes sense for the commands to raise SystemExit.)
        log.debug("Caught exit recommendation", exc_info=e)
        log.critical(str(e))
        # pylint: disable=W0707
        raise SystemExit(1)
    except DownstreamError as e:
        # For these operations, we don't want to swallow the exception
        log.debug("Caught downstream error", exc_info=e)
        print("=== Caught downstream error ===", file=sys.stderr)
        print(str(e.__cause__), file=sys.stderr)
        print("---", file=sys.stderr)
        print(
            "If debugging indicates this is a possible error with this program,",
            file=sys.stderr,
        )
        print(
            "please report the issue to the team and include the log file 'rpdk.log'.",
            file=sys.stderr,
        )
        print(
            "Issue tracker: "
            "https://github.com/aws-cloudformation/aws-cloudformation-rpdk/issues",
            file=sys.stderr,
        )
        # pylint: disable=W0707
        raise SystemExit(2)
    except Exception:  # pylint: disable=broad-except
        print("=== Unhandled exception ===", file=sys.stderr)
        print("Please report this issue to the team.", file=sys.stderr)
        print(
            "Issue tracker: github.com/aws-cloudformation/cloudformation-cli/issues",
            file=sys.stderr,
        )

        if log:
            print("Please include the log file 'rpdk.log'", file=sys.stderr)
            log.debug("Unhandled exception", exc_info=True)
        else:
            print("Please include this information:", file=sys.stderr)
            import traceback  # pylint: disable=import-outside-toplevel

            traceback.print_exc()
        # pylint: disable=W0707
        raise SystemExit(EXIT_UNHANDLED_EXCEPTION)