def from_options()

in client/python/cli/command/__init__.py [0:0]


    def from_options(options: argparse.Namespace) -> 'Command':

        def options_get(key, f=lambda x: x):
            return f(getattr(options, key)) if hasattr(options, key) else None

        properties = Parser.parse_properties(options_get(Arguments.PROPERTY))
        set_properties = Parser.parse_properties(options_get(Arguments.SET_PROPERTY))
        remove_properties = options_get(Arguments.REMOVE_PROPERTY)

        command = None
        if options.command == Commands.CATALOGS:
            from cli.command.catalogs import CatalogsCommand
            command = CatalogsCommand(
                options_get(f'{Commands.CATALOGS}_subcommand'),
                catalog_type=options_get(Arguments.TYPE),
                remote_url=options_get(Arguments.REMOTE_URL),
                default_base_location=options_get(Arguments.DEFAULT_BASE_LOCATION),
                storage_type=options_get(Arguments.STORAGE_TYPE),
                allowed_locations=options_get(Arguments.ALLOWED_LOCATION),
                role_arn=options_get(Arguments.ROLE_ARN),
                external_id=options_get(Arguments.EXTERNAL_ID),
                user_arn=options_get(Arguments.USER_ARN),
                region=options_get(Arguments.REGION),
                tenant_id=options_get(Arguments.TENANT_ID),
                multi_tenant_app_name=options_get(Arguments.MULTI_TENANT_APP_NAME),
                consent_url=options_get(Arguments.CONSENT_URL),
                service_account=options_get(Arguments.SERVICE_ACCOUNT),
                catalog_name=options_get(Arguments.CATALOG),
                properties={} if properties is None else properties,
                set_properties={} if set_properties is None else set_properties,
                remove_properties=[] if remove_properties is None else remove_properties
            )
        elif options.command == Commands.PRINCIPALS:
            from cli.command.principals import PrincipalsCommand
            command = PrincipalsCommand(
                options_get(f'{Commands.PRINCIPALS}_subcommand'),
                type=options_get(Arguments.TYPE),
                principal_name=options_get(Arguments.PRINCIPAL),
                client_id=options_get(Arguments.CLIENT_ID),
                principal_role=options_get(Arguments.PRINCIPAL_ROLE),
                properties={} if properties is None else properties,
                set_properties={} if set_properties is None else set_properties,
                remove_properties=[] if remove_properties is None else remove_properties
            )
        elif options.command == Commands.PRINCIPAL_ROLES:
            from cli.command.principal_roles import PrincipalRolesCommand
            command = PrincipalRolesCommand(
                options_get(f'{Commands.PRINCIPAL_ROLES}_subcommand'),
                principal_role_name=options_get(Arguments.PRINCIPAL_ROLE),
                principal_name=options_get(Arguments.PRINCIPAL),
                catalog_name=options_get(Arguments.CATALOG),
                catalog_role_name=options_get(Arguments.CATALOG_ROLE),
                properties={} if properties is None else properties,
                set_properties={} if set_properties is None else set_properties,
                remove_properties=[] if remove_properties is None else remove_properties
            )
        elif options.command == Commands.CATALOG_ROLES:
            from cli.command.catalog_roles import CatalogRolesCommand
            command = CatalogRolesCommand(
                options_get(f'{Commands.CATALOG_ROLES}_subcommand'),
                catalog_name=options_get(Arguments.CATALOG),
                catalog_role_name=options_get(Arguments.CATALOG_ROLE),
                principal_role_name=options_get(Arguments.PRINCIPAL_ROLE),
                properties={} if properties is None else properties,
                set_properties={} if set_properties is None else set_properties,
                remove_properties=[] if remove_properties is None else remove_properties
            )
        elif options.command == Commands.PRIVILEGES:
            from cli.command.privileges import PrivilegesCommand
            subcommand = options_get(f'{Commands.PRIVILEGES}_subcommand')
            command = PrivilegesCommand(
                subcommand,
                action=options_get(f'{subcommand}_subcommand'),
                catalog_name=options_get(Arguments.CATALOG),
                catalog_role_name=options_get(Arguments.CATALOG_ROLE),
                namespace=options_get(Arguments.NAMESPACE, lambda s: s.split('.') if s else None),
                view=options_get(Arguments.VIEW),
                table=options_get(Arguments.TABLE),
                privilege=options_get(Arguments.PRIVILEGE),
                cascade=options_get(Arguments.CASCADE)
            )
        elif options.command == Commands.NAMESPACES:
            from cli.command.namespaces import NamespacesCommand
            subcommand = options_get(f'{Commands.NAMESPACES}_subcommand')
            command = NamespacesCommand(
                subcommand,
                catalog=options_get(Arguments.CATALOG),
                namespace=options_get(Arguments.NAMESPACE, lambda s: s.split('.')),
                parent=options_get(Arguments.PARENT, lambda s: s.split('.') if s else None),
                location=options_get(Arguments.LOCATION),
                properties=properties
            )
        elif options.command == Commands.PROFILES:
            from cli.command.profiles import ProfilesCommand
            subcommand = options_get(f'{Commands.PROFILES}_subcommand')
            command = ProfilesCommand(
                subcommand,
                profile_name=options_get(Arguments.PROFILE)
            )

        if command is not None:
            command.validate()
            return command
        else:
            raise Exception("Please specify a command or run ./polaris --help to view the available commands")