8
내 자신의 사용자 지정 작업에서 ArgumentTypeError 예외를 throw하고 argparse가 나를 잡아내는 것이 가장 좋은 방법은 무엇입니까?사용자 지정 작업에서 ArgumentTypeError 예외 잡기
argparse의 try/except 블록이 내 사용자 지정 동작에 대해이 예외를 처리하지 않는 것 같습니다. 그것의 내장 된 행동에 대해서만 괜찮습니다.
class unique_server_endpoints(argparse.Action):
"""This class avoids having two duplicate OuterIPs in the client argument list"""
def __call__(self, parser, namespace, values, option_string=None):
ips = set()
endpoints = []
for r in values:
ep = server_endpoint(r)
if ep[0] in ips:
raise argparse.ArgumentTypeError("Duplicate OuterIPs found")
else:
ips.add(ep[0])
endpoints.append(ep)
setattr(namespace, self.dest, endpoints)
예를 들어
group.add_argument('-c', "--client", nargs = 2,
dest = "servers", action = unique_server_endpoints,
, 내가 중복 IP를했을 경우 위의 코드에 다음을 제외하고는 아래로
주요 함수 가을과 추악한 스택 트레이스를 인쇄합니다. 나는 그것을 원하지 않으며 어느 쪽도 try/except 블록을 안에 넣고 싶지 않다.
메인.
을 나머지를 여전히 것 argparse :
내가해야 , 나는 단지 "고맙다"는 코멘트를 써서는 안된다. 그러나이 틈새 질문은 정확히 내가 가진 것입니다 - 그리고 OP의 답은 그것을 못 박았습니다! :-) – halloleo