Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: getopt() function |
| Date | 2016-04-15 08:48 -0700 |
| Organization | None to speak of |
| Message-ID | <lnshymna9u.fsf@kst-u.example.com> (permalink) |
| References | <c974cd17-7fe1-44db-9339-7bffd221378e@googlegroups.com> <2as1hb9fsg6u6b4b1k08bv5at5v2eerfno@4ax.com> |
Geoff <geoff@invalid.invalid> writes:
> It's a horrible function, don't use it. You are much better off
> examining command line arguments and parsing them in your program
> explicitly.
Whatever its flaws, getopt() is standard (POSIX, not ISO C), and using
means that your program's handling of command-line arguments is likely
to be consistent with that of other programs. If each program reinvents
the wheel by implementing its own option handling, you're likely to have
gratuitous inconsistencies.
> The example given on that web page is flawed and doesn't print Usage
> unless an unexpected option is used. It should print usage when no
> options are used as well.
Not providing any *options* is valid (that's why they're called
"options"), and shouldn't result in an error message. Not providing any
*arguments* is an error for this particular program, and I agree that it
should result in a usage message rather than just an error message.
> Furthermore, it doesn't properly handle -t
> since it prints error "expected argument after options" whether you
> use -t, -t3, -t 3 on command line.
The program requires zero or more *options* followed by at least one
argument that is not an option. (It could be a file name, for example.)
The "expected argument after options" message is about the lack of an
argument after the options, not about the lack of an argument to the -t
option.
If you run
./getopt_demo -t
it complains that "-t" requires an argument. If you run
./getopt_demo -t arg
it takes "arg" as the argument to the "-t" option, and then complains
that there's no argument after the options. If you run
./getopt_demo -t42 arg
or
./getopt_demo -t 42 arg
it takes 42 as the argument to the "-t" option, and "arg" as the
final non-option program argument.
The program is a bit terse (for example the use of "tfnd" to
indicate whether the "-t" option was found on the command line).
It's also a bit weak on error checking (the argument to -t should
be an integer but it doesn't check that), but I suggest that would
clutter the code.
If you want to improve the example, you can submit a suggestion to
the Linux man-pages project, <https://www.kernel.org/doc/man-pages/>.
I might do so myself.
> If the man page examples can't use it correctly, how can you expect to
> use it correctly?
>
> I don't think Richard Heathfield was joking. I don't think the man
> page author(s) understand getopt either.
I think you've misunderstood the example in the man page. I agree that
the example could be improved.
There's another usage example in the POSIX documentation:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getopt.html
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
getopt() function Alla _ <modelling.data@gmail.com> - 2016-04-15 05:05 -0700
Re: getopt() function Richard Heathfield <rjh@cpax.org.uk> - 2016-04-15 13:09 +0100
Re: getopt() function Alla _ <modelling.data@gmail.com> - 2016-04-15 05:25 -0700
Re: getopt() function gpietsch08618@gmail.com - 2016-04-26 22:39 -0700
Re: getopt() function Alla _ <modelling.data@gmail.com> - 2016-04-27 00:09 -0700
Re: getopt() function gpietsch08618@gmail.com - 2016-05-31 05:44 -0700
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 06:58 -0700
Re: getopt() function Alla _ <modelling.data@gmail.com> - 2016-04-15 07:54 -0700
Re: getopt() function Richard Heathfield <rjh@cpax.org.uk> - 2016-04-15 15:58 +0100
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 08:24 -0700
Re: getopt() function Keith Thompson <kst-u@mib.org> - 2016-04-15 08:48 -0700
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 09:29 -0700
Re: getopt() function Keith Thompson <kst-u@mib.org> - 2016-04-15 11:20 -0700
Re: getopt() function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-04-16 16:51 -0700
Re: getopt() function janus <emekamicro@gmail.com> - 2016-05-31 22:23 -0700
Re: getopt() function Malcolm McLean <malcolm.mclean5@btinternet.com> - 2016-05-31 23:27 -0700
Re: getopt() function janus <emekamicro@gmail.com> - 2016-06-01 00:27 -0700
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 08:37 -0700
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 08:46 -0700
Re: getopt() function Keith Thompson <kst-u@mib.org> - 2016-04-15 12:11 -0700
Re: getopt() function Geoff <geoff@invalid.invalid> - 2016-04-15 13:43 -0700
Re: getopt() function Ian Collins <ian-news@hotmail.com> - 2016-04-16 08:34 +1200
Re: getopt() function Alla _ <modelling.data@gmail.com> - 2016-04-18 08:40 -0700
Re: getopt() function Randy Howard <rhoward.mx@EverybodyUsesIt.com> - 2016-04-18 15:20 -0500
Re: getopt() function boon <fred900rbc@gmail.com> - 2016-04-18 06:26 -0700
csiph-web