Case insensitive argparse choices

前端 未结 3 1175
鱼传尺愫
鱼传尺愫 2020-12-15 02:35

Is it possible to check argparse choices in case-insensitive manner?

import argparse
choices = [\"win64\", \"win32\"]
parser = argparse.ArgumentParser()
pars         


        
3条回答
  •  悲哀的现实
    2020-12-15 02:59

    Transform the argument into lowercase by using

    type = str.lower
    

    for the -p switch.

    This solution was pointed out by chepner in a comment. The solution I proposed earlier was

    type = lambda s : s.lower()
    

    which is also valid, but it's simpler to just use str.lower.

提交回复
热议问题