rc-list - mailing list for the rc(1) shell
 help / color / mirror / Atom feed
* how to 'getopts' in rc
@ 1991-09-05 19:02 malte
  1991-09-06 13:01 ` John Mackin
  0 siblings, 1 reply; 3+ messages in thread
From: malte @ 1991-09-05 19:02 UTC (permalink / raw)
  To: rc

In my dark past (sh) I frequently used "getopts", which is a sh builtin.
Nomen est omen, it can be used the same way as getopt(3).
Now using rc, the nice feature has vanished. So question is:

How would you do it in rc or should getopts be a rc builtin?


_______________________________________________________________________________
malte@techfak.uni-bielefeld.de

send mail to:	Universitaet Bielefeld, Technische Fakultaet
		z. Hd. Malte Uhl
		Postfach 8640
		4800 Bielefeld 1



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to 'getopts' in rc
  1991-09-05 19:02 how to 'getopts' in rc malte
@ 1991-09-06 13:01 ` John Mackin
  0 siblings, 0 replies; 3+ messages in thread
From: John Mackin @ 1991-09-06 13:01 UTC (permalink / raw)
  To: The rc Mailing List

Could anyone on the list who wants to send me mail privately please
note my new address.  The address I was at previously is no longer
valid (and I mean that -- it will not forward).

Malte asks:

    How would you do it in rc or should getopts be a rc builtin?

Well, I think no one on the list would seriously campaign to have a
getopts builtin (at least, I _sincerely hope_ no one would).  It would
be easy, if a little tedious, to code up a getopts rough-equivalent;
one could either take an approach of doing most of the work in rc,
or of making it a function that wrapped around getopt(1) (which is
easily available in the public domain) and got that to do most of
the work.

But hey!  This is rc!  Why hamstring ourselves with old-style interfaces,
when we can do much cuter things -- things that were only _theoretically_
possible in sh, since in any attempt to implement them in practice,
the quoting would have gotten totally out of control.

I have in mind a function along the following rough lines:

	* = ` { getopts opt1 'code1' opt2 'code2' ... - $* }

where each fragment of rc code gets executed if the corresponding
option is present.  The assignment to * is necessary, since a function
can't affect the $* of the enclosing level.  So, for example, if
you have options x and q, and option f that takes an option argument,
you might write:

	* = ` { getopts x 'dashx = true' q 'dashq = true' \
			f: 'dashf = $optarg' - $* }

I think that's a much nicer interface than the getopt(1) style
supported by the getopts builtin, mainly because the option letter and the
code for the option are right next to each other.  I haven't coded this
up in rc (I'm unemployed right now, and I don't have much computer
access), but I don't see any reason why it should be at all difficult.

The only thing that's not perfectly nice, in my view, about this
is the fact you have to explicitly pass $*, and explicitly assign to
it; that, however, is the price that must be paid for getopts not
being a builtin, and I am sure we are all quite willing to pay it.

OK,
John.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: how to 'getopts' in rc
@ 1991-09-09 18:45 malte
  0 siblings, 0 replies; 3+ messages in thread
From: malte @ 1991-09-09 18:45 UTC (permalink / raw)
  To: rc

Today I read the first answers to my last letter on how to "getopts".
I like what John Mackin wrote, but I believe there ar two things to add:

	This is rc!  Why hamstring ourselves with old-style interfaces,

I agree with you that commandline conventions are probably not the ultimate
ones, but here at my site, there are lots of beginners and it won't make
things easier if we change that "old-style" interfaces.
And after all, I am quite comfortable with these. What says POSIX about it?

	* = ` { getopts opt1 'code1' opt2 'code2' ... - $* }

Also a good idea that didn't come to my mind! But I have lots of sh scripts
here which will occasionally be ported to rc. Your suggestion won't make
things easier.


_______________________________________________________________________________
malte@techfak.uni-bielefeld.de

send mail to:	Universitaet Bielefeld, Technische Fakultaet
		z. Hd. Malte Uhl
		Postfach 8640
		4800 Bielefeld 1



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1991-09-09 19:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1991-09-05 19:02 how to 'getopts' in rc malte
1991-09-06 13:01 ` John Mackin
1991-09-09 18:45 malte

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).