Numeric command-line options vs. negative-number arguments

Steven Bethard steven.bethard at gmail.com
Fri Sep 28 09:51:49 EDT 2007


Ben Finney wrote:
> Steven Bethard <steven.bethard at gmail.com> writes:
>> Argparse knows what your option flags look like, so if you specify
>> one, it knows it's an option.  Argparse will only interpret it as a
>> negative number if you specify a negative number that doesn't match
>> a known option.
> 
> That's also irritating, and violates the expected behaviour. It leads
> to *some* undefined options being flagged as errors, and others
> interpreted as arguments. The user shouldn't need to know the complete
> set of options to know which leading-hyphen arguments will be treated
> as options and which ones won't.
> 
> The correct behaviour would be to *always* interpret an argument that
> has a leading hyphen as an option (unless it follows an explicit '--'
> option), and complain if the option is unknown.

It was decided that practicality beats purity here. Arguments with 
leading hyphens which look numeric but aren't in the parser are 
interpreted as negative numbers. Arguments with leading hyphens which 
don't look numeric and aren't in the parser raise errors. Sure, it's not 
the pure answer, but it's the practical answer: "-123" is much more 
likely to be a negative number than an option.

STeVe



More information about the Python-list mailing list