def run_app(**kwargs):
    sys_args = sys.argv
    runner_name = os.path.basename(sys_args[0])
    default_config_path = kwargs.get('default_config_path')
    parser = management.CommandParser(description=kwargs.pop('description'), formatter_class=VerboseHelpFormatter, add_help=False)
    parser.add_argument('-c', '--config', metavar='CONFIG', help='Path to the configuration file')
    parser.add_argument('--version', action='version', version=__version__)
    (args, unparsed_args) = parser.parse_known_args()
    subparsers = parser.add_subparsers(help=False, dest='command', metavar='')
    init_parser = subparsers.add_parser('init', help='Initialize a new configuration')
    init_parser.add_argument('config_path', default=default_config_path, nargs='?', help='Path to output generated configuration file')
    try:
        args = parser.parse_args()
        command = args.command
        command_args = sys.argv[1:]
    except management.CommandError as err:
        if ('invalid choice' not in str(err)):
            raise
        sys_args = (sys_args[:1] + unparsed_args)
        (_, command, command_args) = parse_command_args(sys_args[1:])
    if (not command):
        parser.print_help()
        parser.exit(1)
    if (command == 'init'):
        settings_initializer = kwargs.get('settings_initializer')
        config_path = os.path.expanduser(args.config_path)
        if os.path.exists(config_path):
            print(f'A configuration already exists at {config_path}. Please backup and remove it or choose another path.')
            return
        try:
            create_default_settings(config_path, settings_initializer)
        except OSError as e:
            raise e.__class__(('Unable to write default settings file to %r' % config_path))
        print(('Configuration file created at %r' % config_path))
        return
    config_path = args.config
    try:
        configure_app(config_path=config_path, **kwargs)
    except ValueError as err:
        parser.exit(status=2, message=(str(err) + '\n'))
    management.execute_from_command_line(([runner_name, command] + command_args))
    sys.exit(0)
def run_app(**kwargs):
    sys_args = sys.argv
    runner_name = os.path.basename(sys_args[0])
    default_config_path = kwargs.get('default_config_path')
    parser = management.CommandParser(description=kwargs.pop('description'), formatter_class=VerboseHelpFormatter, add_help=False)
    parser.add_argument('-c', '--config', metavar='CONFIG', help='Path to the configuration file')
    parser.add_argument('--version', action='version', version=__version__)
    (args, unparsed_args) = parser.parse_known_args()
    subparsers = parser.add_subparsers(help=False, dest='command', metavar=)
    init_parser = subparsers.add_parser('init', help='Initialize a new configuration')
    init_parser.add_argument('config_path', default=default_config_path, nargs='?', help='Path to output generated configuration file')
    try:
        args = parser.parse_args()
        command = args.command
        command_args = sys.argv[1:]
    except management.CommandError as err:
        if ('invalid choice' not in str(err)):
            raise
        sys_args = (sys_args[:1] + unparsed_args)
        (_, command, command_args) = parse_command_args(sys_args[1:])
    if (not command):
        parser.print_help()
        parser.exit(1)
    if (command == 'init'):
        settings_initializer = kwargs.get('settings_initializer')
        config_path = os.path.expanduser(args.config_path)
        if os.path.exists(config_path):
            print(f'A configuration already exists at {config_path}. Please backup and remove it or choose another path.')
            return
        try:
            create_default_settings(config_path, settings_initializer)
        except OSError as e:
            raise e.__class__(('Unable to write default settings file to %r' % config_path))
        print(('Configuration file created at %r' % config_path))
        return
    config_path = args.config
    try:
        configure_app(config_path=config_path, **kwargs)
    except ValueError as err:
        parser.exit(status=2, message=(str(err) + '\n'))
    management.execute_from_command_line(([runner_name, command] + command_args))
    sys.exit(0)