Comma separated inputs instead of space separated inputs for argparse

前端 未结 5 968
灰色年华
灰色年华 2021-01-19 02:30

I\'m using argparse to receive inputs from the command line to run my script.

My current input string looks like this:

path> python &         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-19 03:17

    If you're okay with space not comma separated, then it's builtin to argparse:

    In [1]: from argparse import ArgumentParser
    
    In [2]: parser = ArgumentParser()
    
    In [3]: parser.add_argument('-a', nargs='+')
    Out[3]: _StoreAction(option_strings=['-a'], dest='a', nargs='+', const=None, 
                         default=None, type=None, choices=None, help=None, metavar=None)
    
    In [4]: parser.parse_args(['-a', 'foo', 'bar'])
    Out[4]: Namespace(a=['foo', 'bar'])
    

提交回复
热议问题