Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[ksh93] How to use getopts' *listof* attribute?

75 views
Skip to first unread message

Clark J. Wang

unread,
Dec 21, 2011, 9:28:33 AM12/21/11
to
For example:

$ cat foo.sh
optstring="[-]a:[ATTRIBUTE:listof]{[a?Attribute A][b?Attribute B]}"
while getopts "$optstring" opt; do
echo "$opt=$OPTARG"
done
$ ksh foo.sh --man
SYNOPSIS
foo.sh [ options ]

OPTIONS
-a ATTRIBUTE
a Attribute A
b Attribute B
$ ksh foo.sh -a a
a=a
$ ksh foo.sh -a a,b
foo.sh: -a: a,b: unknown option argument value
?=0
Usage: foo.sh [-a ATTRIBUTE]
$ ksh foo.sh -a a:b
foo.sh: -a: a:b: unknown option argument value
?=0
Usage: foo.sh [-a ATTRIBUTE]
$ ksh foo.sh -a 'a;b'
foo.sh: -a: a;b: unknown option argument value
?=0
Usage: foo.sh [-a ATTRIBUTE]
$ ksh foo.sh -a 'a b'
foo.sh: -a: a b: unknown option argument value
?=0
Usage: foo.sh [-a ATTRIBUTE]
$

So what should be the correct syntax of a *list* here?

Dave Gibson

unread,
Dec 23, 2011, 4:55:20 PM12/23/11
to
Clark J. Wang <dear...@gmail.com> wrote:
> For example:
>
> $ cat foo.sh
> optstring="[-]a:[ATTRIBUTE:listof]{[a?Attribute A][b?Attribute B]}"
> while getopts "$optstring" opt; do
> echo "$opt=$OPTARG"
> done

[...]

> $ ksh foo.sh -a a
> a=a
> $ ksh foo.sh -a a,b
> foo.sh: -a: a,b: unknown option argument value
> ?=0
> Usage: foo.sh [-a ATTRIBUTE]

[...]

> So what should be the correct syntax of a *list* here?

A similar question was asked on the ast-users mailing list a few years
ago. Here's the relevant message:

#v+

[ast-users] getopts question
Glenn Fowler gsf at research.att.com
Wed Aug 9 14:35:19 EDT 2006

On Wed, 09 Aug 2006 13:46:16 -0400 Mario DeFazio wrote:
> I have gotten a bit further by using 2-digit numbers:

> [S:server?Operate on one or more of the specified
> \asubservices\a:]:[subservice:listof:=pmserver,repserver,notifyd] \
> {
> [31:pmserver]
> [32:repserver]
> [33:notifyd]
> }

> However, I can't specify a list of more than one service:

> $ ksh93q infa_ctl --server pmserver,repserver stop devrepdb
> infa_ctl: --server: pmserver,repserver: unknown option argument value

> I thought the 'listof' modifier supported this. What am I doing wrong?

the 'listof' refers to the option list declaration, not usage
the usage would have to be through multiple --server options
--server=pmserver --server=repserver
probably with a note on the meaning of multiple --server options

#v-

The original message is available at

<https://mailman.research.att.com/pipermail/ast-users/2006q3/001247.html>

See the "Previous message" links for the rest of the thread.

Clark J. Wang

unread,
Dec 24, 2011, 9:45:56 AM12/24/11
to
I read through all the 4 messages of the thread but I still did not understand, including oneof and ignorecause syntax. My bad English! :( Could you kindly give a simple example?

Dave Gibson

unread,
Dec 24, 2011, 4:21:46 PM12/24/11
to
Clark J. Wang <dear...@gmail.com> wrote:
> On Saturday, December 24, 2011 5:55:20 AM UTC+8, Dave Gibson wrote:
>> Clark J. Wang <dear...@gmail.com> wrote:
>> > For example:
>> >
>> > $ cat foo.sh
>> > optstring="[-]a:[ATTRIBUTE:listof]{[a?Attribute A][b?Attribute B]}"
>> > while getopts "$optstring" opt; do
>> > echo "$opt=$OPTARG"
>> > done
>>
>> [...]
>>
>> > $ ksh foo.sh -a a
>> > a=a
>> > $ ksh foo.sh -a a,b
>> > foo.sh: -a: a,b: unknown option argument value
>> > ?=0
>> > Usage: foo.sh [-a ATTRIBUTE]
>>
>> [...]
>>
>> > So what should be the correct syntax of a *list* here?
>>
>> A similar question was asked on the ast-users mailing list a few years
>> ago. Here's the relevant message:
[...]
>> the 'listof' refers to the option list declaration, not usage

> I read through all the 4 messages of the thread but I still did not
> understand, including oneof and ignorecause syntax. My bad English!

Your original use (in foo.sh) was correct. listof does not appear to
affect getopts' operation. It is, however, visible in the output of
some help requests.

Note the ".OP" line in the following (using the example foo.sh above):

$ ksh93 foo.sh --??api
.SH SYNOPSIS
foo.sh [ options ]
.SH OPTIONS
.OP a - string:listof ATTRIBUTE
.H2 a
Attribute A
.H2 b
Attribute B
.SH IMPLEMENTATION
.SP


As listof does not make it possible to use several option-arguments
with a single option you'll need to specify the option multiple times,
once for each option-argument:

foo.sh -a a -a b

Clark J. Wang

unread,
Dec 24, 2011, 9:43:58 PM12/24/11
to
On Sunday, December 25, 2011 5:21:46 AM UTC+8, Dave Gibson wrote:
> Clark J. Wang <dear...@gmail.com> wrote:
> > On Saturday, December 24, 2011 5:55:20 AM UTC+8, Dave Gibson wrote:
Thanks a lot for the clarification.
0 new messages