사용자 도구

사이트 도구


argparse

문서의 이전 판입니다!


argparse

임의의 타입 parsing 하기

예) bool

def str2bool(v):
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')
 
...
 
parser.add_argument("--nice", type=str2bool, nargs='?',
                        const=True, default=NICE,
                        help="Activate nice mode.")
 
...
 
script --nice
script --nice <bool>
argparse.1542590232.txt.gz · 마지막으로 수정됨: (바깥 편집)