Qs on options parsing

Qs on options parsing

Post by kj » Thu, 08 Dec 2005 10:12:38



I'm having a hard time understanding some of the guidelines in

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.htm...

Guideline 8 says (my emphasis):

    When multiple option-arguments are specified to follow a single
    option, they should be presented as a single argument, using
                        ^^^^^^^^^^^^
    commas within that argument or <blank>s within that argument
    to separate them.

I'm mystified by the underlined words.  What does "presented" mean
in this context?  Presented to whom and by whom?  Who/what is
responsible for enforcing this guideline?  If option -x of utility
foo accepts multiple arguments, and one issues the following command

  % foo -x bar baz frobozz

are "baz" and "frobozz" passed along with "bar" as part of the
multiple arguments of "-x"?  Or does need something like this:

  % foo -x 'bar baz frobozz'

or

  % foo -x bar,baz,frobozz

?

Guideline 3 includes the following:

    The -W (capital-W) option shall be reserved for vendor options.

What are examples of the types of vendors this guideline refers
to?  Does anyone know of a real-world example of the use of this
guideline?

Guideline 6 refers to item (2) of

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.htm...

which I can't make any sense of at all.  (BTW, I thought the answers
to all my questions on this item would be simply matters of
definition, but I didn't find any helpful definitions in the
Definitions section, at

http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html

)  This item says:

    2. Option-arguments are sometimes shown separated from their
       options by <blank>s, sometimes directly adjacent. This
       reflects the situation that in some cases an option-argument
       is included within the same argument string as the option;
       in most cases it is the next argument. The Utility Syntax
       Guidelines in Utility Syntax Guidelines require that the
       option be a separate argument from its option-argument, but
       there are some exceptions in IEEE Std 1003.1-2001 to ensure
       continued operation of historical applications:

       a. If the SYNOPSIS of a standard utility shows a <space>
          between an option and option-argument (as with [ -c
          option_argument] in the example), a conforming application
          shall use separate arguments for that option and its
          option-argument.

       b. If a <space> is not shown (as with [ -f option_argument]
          in the example), a conforming application shall place an
          option and its option-argument directly adjacent in the
          same argument string, without intervening <blank>s.

       c. Notwithstanding the preceding requirements on conforming
          applications, a conforming implementation shall permit
          an application to specify options and option-arguments
          as a single argument or as separate arguments whether or
          not a <space> is shown on the synopsis line, [XSI] [Option
          Start] except in those cases (marked with the XSI
          portability warning) where an option-argument is optional
          and no separation can be used. [Option End]

       d. A standard utility may also be implemented to operate
          correctly when the required separation into multiple
          arguments is violated by a non-conforming application.

Part (a) seems to suggest that a program should comply with the
stipulations of the documentation (i.e. its SYNOPSIS) which sounds
backwards to me.  In other words, I would have written that the
SYNOPSIS should reflect the behavior of the program not the other
way around; the program is the "independent variable" IMO, not the
SYNOPSIS.

Part (c) refers to "conforming applications" and "conforming
implementations".  What exactly is the difference between these
two?  I imagine that "applications" here refers to utilities, which
is the subject of the whole chapter (12).  But, what does "conforming
implementations" refer to?

Any clues on any of the questions above would be much appreciated.

Many thanks in advance!

kj

--
NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

 
 
 

Qs on options parsing

Post by Chris F.A. Johnso » Thu, 08 Dec 2005 10:32:34



> I'm having a hard time understanding some of the guidelines in

> http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.htm...

> Guideline 8 says (my emphasis):

>     When multiple option-arguments are specified to follow a single
>     option, they should be presented as a single argument, using
>                         ^^^^^^^^^^^^
>     commas within that argument or <blank>s within that argument
>     to separate them.

> I'm mystified by the underlined words.  What does "presented" mean
> in this context?  Presented to whom and by whom?

    Presented by the caller (on the command line) to the command.

Quote:> Who/what is responsible for enforcing this guideline?

    The code that parses the command line, e.g., getopts.

Quote:> If option -x of utility foo accepts multiple arguments, and one
> issues the following command

>  % foo -x bar baz frobozz

> are "baz" and "frobozz" passed along with "bar" as part of the
> multiple arguments of "-x"?

     Only bar is an argument to -x.

Quote:> Or does need something like this:

>  % foo -x 'bar baz frobozz'

> or

>  % foo -x bar,baz,frobozz

    Use whichever the command requires.

--
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence

 
 
 

Qs on options parsing

Post by Stephane CHAZELA » Fri, 09 Dec 2005 04:14:59


2005-12-7, 01:12(+00), kj:

Quote:

> I'm having a hard time understanding some of the guidelines in

> http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.htm...

> Guideline 8 says (my emphasis):

>     When multiple option-arguments are specified to follow a single
>     option, they should be presented as a single argument, using
>                         ^^^^^^^^^^^^
>     commas within that argument or <blank>s within that argument
>     to separate them.

Like for ps -o

ps -o comm,pid,ppid

or ps -o "comm pid ppid"

[...]

Quote:>     The -W (capital-W) option shall be reserved for vendor options.

> What are examples of the types of vendors this guideline refers
> to?  Does anyone know of a real-world example of the use of this
> guideline?

See gawk and its -W ... options

[...]

Quote:>        a. If the SYNOPSIS of a standard utility shows a <space>
>      between an option and option-argument (as with [ -c
>      option_argument] in the example), a conforming application
>      shall use separate arguments for that option and its
>      option-argument.
> Part (a) seems to suggest that a program should comply with the
> stipulations of the documentation (i.e. its SYNOPSIS) which sounds
> backwards to me.  In other words, I would have written that the
> SYNOPSIS should reflect the behavior of the program not the other
> way around; the program is the "independent variable" IMO, not the
> SYNOPSIS.

No, "application" is not the utility, it's the script one writes
calling that utility.

If the synopsis for an utility is [ -c option_argument ],
in your script, you should prefer:

this-utility -c foo

to

this-utility -cfoo

Quote:>        c. Notwithstanding the preceding requirements on conforming
>      applications, a conforming implementation shall permit
>      an application to specify options and option-arguments
>      as a single argument or as separate arguments whether or
>      not a <space> is shown on the synopsis line, [XSI] [Option
>      Start] except in those cases (marked with the XSI
>      portability warning) where an option-argument is optional
>      and no separation can be used. [Option End]

> Part (c) refers to "conforming applications" and "conforming
> implementations".  What exactly is the difference between these
> two?  I imagine that "applications" here refers to utilities, which
> is the subject of the whole chapter (12).  But, what does "conforming
> implementations" refer to?

[...]

No, see the rationale:
http://www.opengroup.org/onlinepubs/009695399/xrat/xbd_chap03.html

--
Stphane

 
 
 

1. Is it possible to parse a command option-string into option words?

Hello, please help me.

In sh or ksh, I know getopt function may help to parse a command
option string into individual characters. Is there a way to parse a
command option string into option words?

For example I got to run like this:

do_sth.sh -NORMAL abc

I like do_sth.sh to recognize "NORMAL" as an option (and take abc as
some value), against other potential words, such as "URGENT" --
instead of only "N" against "U" that getopt may help do.

Please also let me know if I can do it in either sh, csh or ksh.

Thanks.

2. how to set the prompt under Bourne shell?

3. Parsing command line options in C shell

4. Keypad doesn't work in CDE, but does under OpenWindows?

5. How to parse options in csh

6. /dev/xf86 ? ATI Xpert 98 (mach64) on i386

7. BASH: getopt function that parses long-named options

8. background dead at startup

9. Where is POSIX options parsing standard?

10. Using "getopt" to parse option arguments w/ spaces

11. bash getopts and multi option parsing

12. Using getopt to parse long options

13. parsing options with getopts