<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def cli():
 """An improved shell command, based on konch."""

    from flask.globals import _app_ctx_stack

    app = _app_ctx_stack.top.app
    options = {key: app.config.get(key, DEFAULTS[key]) for key in DEFAULTS.keys()}
    base_context = {"app": app}
    if options["KONCH_FLASK_IMPORTS"]:
        base_context.update(get_flask_imports())

    context = dict(base_context)

    if options["KONCH_FLASK_SHELL_CONTEXT"]:
        flask_context = app.make_shell_context()
        context.update(flask_context)

    context.update(options["KONCH_CONTEXT"])

    def context_formatter(ctx):
        formatted_base = ", ".join(sorted(base_context.keys()))
        ret = "\n{FLASK}\n{base_context}\n".format(
            FLASK=click.style("Flask:", bold=True), base_context=formatted_base
        )
        if options["KONCH_FLASK_SHELL_CONTEXT"]:
            variables = ", ".join(sorted(flask_context.keys()))
            ret += "\n{ADDITIONAL}\n{variables}\n".format(
                ADDITIONAL=click.style(
                    "Flask shell context (see shell_context_processor()):", bold=True
                ),
                variables=variables,
            )
        if options["KONCH_CONTEXT"]:
            variables = ", ".join(sorted(options["KONCH_CONTEXT"].keys()))
            ret += "\n{ADDITIONAL}\n{variables}".format(
                ADDITIONAL=click.style(
                    "Additional variables (see KONCH_CONTEXT):", bold=True
                ),
                variables=variables,
            )
        return ret

    context_format = options["KONCH_CONTEXT_FORMAT"] or context_formatter
    konch.start(
        context=context,
        shell=options["KONCH_SHELL"],
        banner=options["KONCH_BANNER"],
        prompt=options["KONCH_PROMPT"],
        output=options["KONCH_OUTPUT"],
        ptpy_vi_mode=options["KONCH_PTPY_VI_MODE"],
        context_format=context_format,
        ipy_extensions=options["KONCH_IPY_EXTENSIONS"],
        ipy_autoreload=options["KONCH_IPY_AUTORELOAD"],
        ipy_colors=options["KONCH_IPY_COLORS"],
        ipy_highlighting_style=options["KONCH_IPY_HIGHLIGHTING_STYLE"],
    )