TypeError: __init__() got an unexpected keyword argument 'type' in argparse

后端 未结 2 842
被撕碎了的回忆
被撕碎了的回忆 2021-02-05 00:28

Hey so I\'m using argparse to try and generate a quarterly report. This is what the code looks like:

parser  = argparse.ArgumentParser()

parser.add_argument(\'-         


        
2条回答
  •  我在风中等你
    2021-02-05 01:12

    What action="store_true" means is that if the argument is given on the command line then a True value should be stored in the parser. What you actually want is to store the given year (as a string) and quarter (as an int).

    parser  = argparse.ArgumentParser()
    
    parser.add_argument('-q', "--quarter", type=int, help="Enter a Quarter number: 1,2,3, or 4 ")
    parser.add_argument('-y', "--year", type=str, help="Enter a year in the format YYYY ")
    args = parser.parse_args()
    

    When you specify action='store_true argparse is internally instantiating a _StoreAction instance whose constructor does not accept a type parameter (since it will always be a boolean (True/False)). You cannot supply action="store_true" and 'type' at the same time.

提交回复
热议问题