Numeric command-line options vs. negative-number arguments (was: getopt with negative numbers?)

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Sep 27 21:34:01 EDT 2007


Steven Bethard <steven.bethard at gmail.com> writes:

> In most cases, argparse (http://argparse.python-hosting.com/)
> supports negative numbers right out of the box, with no need to use
> '--':
> 
>     >>> import argparse
>     >>> parser = argparse.ArgumentParser()
>     >>> parser.add_argument('-a', type=int)
>     >>> parser.add_argument('b', type=int)
>     >>> args = parser.parse_args('-a -42 -123'.split())
>     >>> args.a
>     -42
>     >>> args.b
>     -123

That would be irritating. I've used many programs which have numbers
for their options because it makes the most sense, e.g. 'mpage' to
indicate number of virtual pages on one page, or any number of
networking commands that use '-4' and '-6' to specify IPv4 or IPv6.

If argparse treats those as numeric arguments instead of options,
that's violating the Principle of Least Astonishment for established
command-line usage (hyphen introduces an option).

-- 
 \       "I went to a general store. They wouldn't let me buy anything |
  `\                                  specifically."  -- Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list