List of arguments with argparse

后端 未结 2 793
时光说笑
时光说笑 2021-01-18 10:42

I\'m trying to pass a list of arguments with argparse but the only way that I\'ve found involves rewriting the option for each argument that I want to pass:

What I c

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 11:47

    Use nargs:

    ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The nargs keyword argument associates a different number of command-line arguments with a single action.

    For example, if nargs is set to '+'

    Just like '*', all command-line args present are gathered into a list. Additionally, an error message will be generated if there wasn’t at least one command-line argument present.

    So, your code would look like

    parser.add_argument('-t', dest='table', help='', nargs='+')
    

    That way -t arguments will be gathered into list automatically (you don't have to explicitly specify the action).

提交回复
热议问题