Hi guys,
to do a proper Linux/unix styled application, what is the best choice (eg. afaik ls uses getopt_long but for example ffmpeg getopt_long_only). Which one do you recommend?
Cheers,
Hi guys,
to do a proper Linux/unix styled application, what is the best choice (eg. afaik ls uses getopt_long but for example ffmpeg getopt_long_only). Which one do you recommend?
Cheers,
I've never used getopt_long_only, but it seems like it would have to do more lookups, as it has to look at both the long and short options if an unknown flag starts with a single -
:
If an option that starts with '-' (not '--') doesn't match a long option, but does match a short option, it is parsed as a short option instead.
-- man getopt_long_only
In my opinion, the following things are usually true:
If a program gets big enough, it will eventually run out of short option combinations that make any sort of sense when compared to the canonical option. For instance, -Z might be the same as a long option that begins with a totally different letter. At that point, especially for a single maintainer, the option parsing code becomes a headache to maintain.
You have a few choices when that happens:
Mix in different locales, and you really begin grasping the pain.
When I sit down to write a tool that takes lots of options, the first thing I usually do is write the code to parse the arguments, this helps to plan the flow of the program and becomes an outline. You just make each option work after that.
In other words, if you get to the point where options become such a chronic pain, its usually indicative of a program that quickly evolved beyond its planning.
Anyway, to bring my long winded answer to a close, its usually better to keep the compat getopt() behavior whenever possible. Code that gets instructions from the user is just the cost of doing business, so your concern should be fully on what makes for a better user experience, when possible.