'Main function.
Parses command, load settings and dispatches accordingly.'

def handle(self, argv=None):

    desc = u'Zappa    -    Deploy    Python    applications    to    AWS    Lambda    and    API    Gateway.\n'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument(u'-v', u'--version', action=u'version', version=pkg_resources.get_distribution(u'zappa').version, help=u'Print    the    zappa    version')
    env_parser = argparse.ArgumentParser(add_help=False)
    me_group = env_parser.add_mutually_exclusive_group()
    all_help = u'Execute    this    command    for    all    of    our    defined    Zappa    stages.'
    me_group.add_argument(u'--all', action=u'store_true', help=all_help)
    me_group.add_argument(u'stage_env', nargs=u'?')
    group = env_parser.add_argument_group()
    group.add_argument(u'-a', u'--app_function', help=u'The    WSGI    application    function.')
    group.add_argument(u'-s', u'--settings_file', help=u'The    path    to    a    Zappa    settings    file.')
    group.add_argument(u'-q', u'--quiet', action=u'store_true', help=u'Silence    all    output.')
    group.add_argument(u'-j', u'--json', action=u'store_true', help=u'Make    the    output    of    this    command    be    machine    readable.')
    group.add_argument(u'--disable_progress', action=u'store_true', help=u'Disable    progress    bars.')
    subparsers = parser.add_subparsers(title=u'subcommands', dest=u'command')
    cert_parser = subparsers.add_parser(u'certify', parents=[env_parser], help=u'Create    and    install    SSL    certificate')
    cert_parser.add_argument(u'--no-cleanup', action=u'store_true', help=u"Don't    remove    certificate    files    from    /tmp    during    certify.    Dangerous.")
    cert_parser.add_argument(u'--manual', action=u'store_true', help=u"Gets    new    Let's    Encrypt    certificates,    but    prints    them    to    console.Does    not    update    API    Gateway    domains.")
    cert_parser.add_argument(u'-y', u'--yes', action=u'store_true', help=u'Auto    confirm    yes.')
    deploy_parser = subparsers.add_parser(u'deploy', parents=[env_parser], help=u'Deploy    application.')
    init_parser = subparsers.add_parser(u'init', help=u'Initialize    Zappa    app.')
    package_parser = subparsers.add_parser(u'package', parents=[env_parser], help=u'Build    the    application    zip    package    locally.')
    package_parser.add_argument(u'-o', u'--output', help=u'Name    of    file    to    output    the    package    to.')
    template_parser = subparsers.add_parser(u'template', parents=[env_parser], help=u'Create    a    CloudFormation    template    for    this    API    Gateway.')
    template_parser.add_argument(u'-l', u'--lambda-arn', required=True, help=u'ARN    of    the    Lambda    function    to    template    to.')
    template_parser.add_argument(u'-r', u'--role-arn', required=True, help=u'ARN    of    the    Role    to    template    with.')
    template_parser.add_argument(u'-o', u'--output', help=u'Name    of    file    to    output    the    template    to.')
    invoke_parser = subparsers.add_parser(u'invoke', parents=[env_parser], help=u'Invoke    remote    function.')
    invoke_parser.add_argument(u'--raw', action=u'store_true', help=u'When    invoking    remotely,    invoke    this    python    as    a    string,    not    as    a    modular    path.')
    invoke_parser.add_argument(u'--no-color', action=u'store_true', help=u"Don't    color    the    output")
    invoke_parser.add_argument(u'command_rest')
    manage_parser = subparsers.add_parser(u'manage', help=u'Invoke    remote    Django    manage.py    commands.')
    rest_help = u'Command    in    the    form    of    <env>    <command>.    <env>    is    not    required    if    --all    is    specified'
    manage_parser.add_argument(u'--all', action=u'store_true', help=all_help)
    manage_parser.add_argument(u'command_rest', nargs=u'+', help=rest_help)
    manage_parser.add_argument(u'--no-color', action=u'store_true', help=u"Don't    color    the    output")
    def positive_int(s):
        u'    Ensure    an    arg    is    positive    '
        i = int(s)
        if (i < 0):
            msg = u'This    argument    must    be    positive    (got    {})'.format(s)
            raise argparse.ArgumentTypeError(msg)
        return i
    rollback_parser = subparsers.add_parser(u'rollback', parents=[env_parser], help=u'Rollback    deployed    code    to    a    previous    version.')
    rollback_parser.add_argument(u'-n', u'--num-rollback', type=positive_int, default=1, help=u'The    number    of    versions    to    rollback.')
    subparsers.add_parser(u'schedule', parents=[env_parser], help=u'Schedule    functions    to    occur    at    regular    intervals.')
    status_parser = subparsers.add_parser(u'status', parents=[env_parser], help=u'Show    deployment    status    and    event    schedules.')
    tail_parser = subparsers.add_parser(u'tail', parents=[env_parser], help=u'Tail    deployment    logs.')
    tail_parser.add_argument(u'--no-color', action=u'store_true', help=u"Don't    color    log    tail    output.")
    tail_parser.add_argument(u'--http', action=u'store_true', help=u'Only    show    HTTP    requests    in    tail    output.')
    tail_parser.add_argument(u'--non-http', action=u'store_true', help=u'Only    show    non-HTTP    requests    in    tail    output.')
    tail_parser.add_argument(u'--since', type=str, default=u'100000s', help=u'Only    show    lines    since    a    certain    timeframe.')
    tail_parser.add_argument(u'--filter', type=str, default=u'', help=u'Apply    a    filter    pattern    to    the    logs.')
    undeploy_parser = subparsers.add_parser(u'undeploy', parents=[env_parser], help=u'Undeploy    application.')
    undeploy_parser.add_argument(u'--remove-logs', action=u'store_true', help=u'Removes    log    groups    of    api    gateway    and    lambda    task    during    the    undeployment.')
    undeploy_parser.add_argument(u'-y', u'--yes', action=u'store_true', help=u'Auto    confirm    yes.')
    subparsers.add_parser(u'unschedule', parents=[env_parser], help=u'Unschedule    functions.')
    subparsers.add_parser(u'update', parents=[env_parser], help=u'Update    deployed    application.')
    subparsers.add_parser(u'shell', parents=[env_parser], help=u'A    debug    shell    with    a    loaded    Zappa    object.')
    argcomplete.autocomplete(parser)
    args = parser.parse_args(argv)
    self.vargs = vars(args)
    if ((args.command == u'manage') and (not self.vargs.get(u'all'))):
        self.stage_env = self.vargs[u'command_rest'].pop(0)
    else:
        self.stage_env = self.vargs.get(u'stage_env')
    if (args.command == u'package'):
        self.load_credentials = False
    self.command = args.command
    self.disable_progress = self.vargs.get(u'disable_progress')
    if self.vargs.get(u'quiet'):
        self.silence()
    if (self.command == u'init'):
        self.init()
        return
    if (not self.vargs.get(u'json')):
        self.check_for_update()
    self.load_settings_file(self.vargs.get(u'settings_file'))
    all_stages = self.vargs.get(u'all')
    stages = []
    if all_stages:
        stages = self.zappa_settings.keys()
    elif (not self.stage_env):
        if (len(self.zappa_settings.keys()) == 1):
            stages.append(list(self.zappa_settings.keys())[0])
        else:
            parser.error(u'Please    supply    an    stage    to    interact    with.')
    else:
        stages.append(self.stage_env)
    for stage in stages:
        try:
            self.dispatch_command(self.command, stage)
        except ClickException as e:
            e.show()
            sys.exit(e.exit_code)
