command line args
Steven Bethard
steven.bethard at gmail.com
Mon Mar 14 17:57:47 EST 2005
joe at gmail.com wrote:
> Hello,
>
> I have the following commands:
> testb -s <size>
> testb -s <size> -o <input file>
> testb -s <size> -o <codes>
>
> How do i split the commands so that all three are valid. And how do i
> check for missing arguments?
Use optparse. It's an improvement over the old getopt that makes
writing option parsers easy:
py> import optparse
py> option_parser = optparse.OptionParser(
... usage='%prog -s <size> [-o <input file|codes>]')
py> option_parser.add_option('-s', metavar='<size>', type='int')
<Option at 0x12b1ee0: -s>
py> option_parser.add_option('-o', metavar='<input file|codes>')
<Option at 0x12b1fd0: -o>
py> options, args = option_parser.parse_args('-s 1 -o data.txt'.split())
py> options.s, options.o
(1, 'data.txt')
py> option_parser.print_help()
usage: evaluation.py -s <size> [-o <input file|codes>]
options:
-h, --help show this help message and exit
-s <size>
-o <input file|codes>
STeVe
More information about the Python-list
mailing list