1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2021-2024 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 18import argparse 19import logging 20from typing import Optional, List, Union 21 22from runner.logger import Log 23 24_LOGGER = logging.getLogger("runner.options.cli_args_wrapper") 25 26 27class CliArgsWrapper: 28 args = None 29 30 @staticmethod 31 def setup(args: argparse.Namespace) -> None: 32 CliArgsWrapper.args = args 33 34 # We use Log.exception_and_raise which throws exception. no need in return 35 # pylint: disable=inconsistent-return-statements 36 @staticmethod 37 def get_by_name(name: str) -> Optional[Union[str, List[str]]]: 38 if name in dir(CliArgsWrapper.args): 39 value = getattr(CliArgsWrapper.args, name) 40 if value is None: 41 return value 42 if isinstance(value, list): 43 return value 44 return str(value) 45 46 Log.exception_and_raise(_LOGGER, f"Cannot recognize CLI option {name} ") 47