getopt with negative numbers?

C

Carl Banks

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.


Carl Banks
 
C

Carl Banks

A user shouldn't have to go out of their way to know whether what they
type on a command line will be treated as an option or an argument.

I guess typing

../program --help

is out of the question.



Carl Banks
 
S

Steven Bethard

Carl said:
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
 
B

Ben Finney

Carl Banks said:
I guess typing
./program --help

is out of the question.

You're trying to have it both ways.

You're saying the user "shouldn't have to go out of their way" to type
arbitrary arguments on the command line. Then, in your next message,
you suggest they must *read the detailed command-line help* in order
to know whether they *can* type arbitrary command-line arguments.

Is "learn about how the program expects options and arguments" within
your definition of "go out of their way", or isn't it? If it's not,
then "shouldn't have to go out of their way" is *not* an argument in
favour of special-casing negative-numbers.
 
C

Carl Banks

You're trying to have it both ways.

You're saying the user "shouldn't have to go out of their way" to type
arbitrary arguments on the command line.

No, I'm not. Don't put words in my mouth.

I don't think it's "going out of your way" to read the documentation,
or to put a couple hyphens on the line. Those were your words, and
they were an utterly silly hyperbole.

Then, in your next message,
you suggest they must *read the detailed command-line help* in order
to know whether they *can* type arbitrary command-line arguments.

You're writing a program that takes numerical positional arguments.

Are you being more of asshole ir you
A. Expect me to read the documentation, or
B. Force me to type two hyphens every single time I want to enter a
negative number?

I would say B by a mile.

Not that it's "going out of my way" to enter the extra hyphens; it's
just irritating and completely, totally, and utterly unnecessary.

Is "learn about how the program expects options and arguments" within
your definition of "go out of their way", or isn't it? If it's not,
then "shouldn't have to go out of their way" is *not* an argument in
favour of special-casing negative-numbers.

It's only a special case in your imagination. Try typing this:

seq -9 -1

On my system, seq is a GNU tool, and the guys at the FSF are some of
the biggest tightwads around when it comes to command line options.
Even when they allow nonconformant options for the sake of backwards
compatibility, they run a guilt trip on you. But even they see the
practicality in allowing negative number arguments in some cases.


Carl Banks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top