1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2023 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import argparse 20 21from exceptions.ohos_exception import OHOSException 22from containers.status import throw_exception 23 24 25class ArgsFactory(): 26 27 @staticmethod 28 @throw_exception 29 def genetic_add_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 30 if arg['arg_type'] == 'bool': 31 return _add_bool_option(parser, arg) 32 elif arg['arg_type'] == 'str': 33 return _add_str_option(parser, arg) 34 elif arg['arg_type'] == 'list': 35 return _add_list_option(parser, arg) 36 elif arg['arg_type'] == 'subparsers': 37 return _add_list_option(parser, arg) 38 else: 39 raise OHOSException('Unknown arg type "{}" for arg "{}"' 40 .format(arg['arg_type'], arg['arg_name']), "0003") 41 42 43def _add_bool_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 44 if arg['arg_attribute'].get('abbreviation'): 45 return _add_bool_abbreviation_option(parser, arg) 46 else: 47 return parser.add_argument(arg['arg_name'], help=arg['arg_help'], nargs='?', 48 default=arg['argDefault']) 49 50 51def _add_str_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 52 if arg['arg_attribute'].get('optional'): 53 if arg['arg_attribute'].get('abbreviation'): 54 return _add_str_optional_abbreviation_option(parser, arg) 55 else: 56 return _add_str_optional_option(parser, arg) 57 elif arg['arg_attribute'].get('abbreviation'): 58 return _add_str_abbreviation_option(parser, arg) 59 else: 60 return parser.add_argument(arg['arg_name'], help=arg['arg_help'], 61 default=arg['argDefault']) 62 63 64def _add_list_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 65 if arg['arg_attribute'].get('abbreviation'): 66 return _add_list_abbreviation_option(parser, arg) 67 else: 68 return parser.add_argument(arg['arg_name'], help=arg['arg_help'], 69 nargs='*', default=arg['argDefault'], action='append') 70 71 72def _add_bool_abbreviation_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 73 return parser.add_argument(arg['arg_attribute'].get('abbreviation'), arg['arg_name'], help=arg['arg_help'], 74 nargs='?', default=arg['argDefault']) 75 76 77def _add_str_abbreviation_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 78 return parser.add_argument(arg['arg_attribute'].get('abbreviation'), arg['arg_name'], help=arg['arg_help'], 79 default=arg['argDefault']) 80 81 82def _add_str_optional_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 83 return parser.add_argument(arg['arg_name'], help=arg['arg_help'], 84 default=arg['argDefault'], choices=arg['arg_attribute'].get('optional')) 85 86 87def _add_str_optional_abbreviation_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 88 return parser.add_argument(arg['arg_attribute'].get('abbreviation'), arg['arg_name'], help=arg['arg_help'], 89 default=arg['argDefault'], choices=arg['arg_attribute'].get('optional')) 90 91 92def _add_list_abbreviation_option(parser: argparse.ArgumentParser, arg: dict) -> argparse.ArgumentParser: 93 return parser.add_argument(arg['arg_attribute'].get('abbreviation'), arg['arg_name'], help=arg['arg_help'], 94 nargs='*', default=arg['argDefault'], action='append') 95