사용자 도구

사이트 도구


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>

bool 타입

command –feature / command –no-feature 방식

parser.add_argument('--feature', dest='feature', action='store_true')
parser.add_argument('--no-feature', dest='feature', action='store_false')
parser.set_defaults(feature=True)
argparse.1542590351.txt.gz · 마지막으로 수정됨: 2024/03/23 02:37 (바깥 편집)