1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2023 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17# This file does only contain a selection of the most common options. For a 18# full list see the documentation: 19# http://www.sphinx-doc.org/en/master/config 20 21import argparse 22import logging 23from typing import Optional, List, Union 24 25from runner.logger import Log 26 27_LOGGER = logging.getLogger("runner.options.cli_args_wrapper") 28 29 30class CliArgsWrapper: 31 args = None 32 33 @staticmethod 34 def setup(args: argparse.Namespace) -> None: 35 CliArgsWrapper.args = args 36 37 @staticmethod 38 def get_by_name(name: str) -> Optional[Union[str, List[str]]]: 39 if name in dir(CliArgsWrapper.args): 40 value = getattr(CliArgsWrapper.args, name) 41 if value is None: 42 return None 43 if isinstance(value, list): 44 return value 45 return str(value) 46 47 Log.exception_and_raise(_LOGGER, f"Cannot recognize CLI option {name} ") 48