# foo.py
import argparse
def args_validation(valid_list, valid_value):
assert valid_value in valid_list, 'invalid {}, valid args:{}'.format(valid_value, '/'.join(valid_list))
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('--trainer-pkg-type')
args = arg_parser.parse_args()
allowed_pkg_type = [
'src',
'whl',
'bin',
]
args_validation(allowed_pkg_type, args.trainer_pkg_type)
python3 foo.py --trainer-pkg-type src
正常运行
python3 foo.py --trainer-pkg-type aaa
-> invalid aaa, valid args:src/whl/bin