Numeric command-line options vs. negative-number arguments
Steven Bethard
steven.bethard at gmail.com
Fri Sep 28 19:36:02 EDT 2007
Carl Banks wrote:
> On Sep 28, 9:51 am, Steven Bethard <steven.beth... at gmail.com> wrote:
>> 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.
>
> What if (for example) you define a option "-3", and also accept
> numerical arguments on the command line. Then you could get sudden
> unexpected behavior if you input the wrong number:
>
> "./hello -1" works ok.
> "./hello -2" works ok.
> "./hello -3" ... whoops, now the negative number is suddenly an
> option.
>
> Granted, it would be stupid for a program to do that, but it suggests
> to me that it's probably a good idea to treat all negative numbers the
> same. I.e. if there are any numerical options, then all negative
> numbers are treated as options. If there are none, then negative
> numbers are treated as numbers.
That's probably a good guideline. Anyone mixing numeric flags with
negative number arguments is asking for an exception of some sort. ;-)
I'm going to let it sit for a while and think about it, but I'll
probably make that change in the next release.
STeVe
More information about the Python-list
mailing list