With Python's optparse module, how do you create an option that takes a variable number of arguments?

后端 未结 6 2163
终归单人心
终归单人心 2021-02-13 19:38

With Perl\'s Getopt::Long you can easily define command-line options that take a variable number of arguments:

foo.pl --files a.txt             --ve         


        
6条回答
  •  再見小時候
    2021-02-13 20:20

    I believe optparse does not support what you require (not directly -- as you noticed, you can do it if you're willing to do all the extra work of a callback!-). You could also do it most simply with the third-party extension argparse, which does support variable numbers of arguments (and also adds several other handy bits of functionality).

    This URL documents argparse's add_argument -- passing nargs='*' lets the option take zero or more arguments, '+' lets it take one or more arguments, etc.

提交回复
热议问题