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

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

Post by Tom Ca » Thu, 09 May 2002 21:11:39



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.

 
 
 

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

Post by Thomas Weidenfell » Thu, 09 May 2002 21:41:09



> For example I got to run like this:

> do_sth.sh -NORMAL abc

First some general remarks:

        - There is no need to us a .sh suffix.

        - I personally don't like '_'. '-' is easier to type on "unix"
          keyboards :-)

        - It is very common to have lowercase options. Much easier to
          type :-)

        - IMHO started by the GNU guys is is not uncommon to use a
          single '-' for one letter options (like '-u') and '--' for
          long options.

So IMHO, it should be

        do-sth --normal abc

Quote:> 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.

For what is /bin/sh on Solaris, the following works with short and log
options, and also recognizes '--' to indicate end-of-options - in case
you have an argument starting with '-'. You can add more consistency
checks to the case statements if you like.

        #!/bin/sh

        #
        # Read options
        #
        while [ $# -gt 0 ] ; do
                case "$1" in
                --normal|-n)
                        if [ $# -lt 2 ] ; then
                                # some error message and exit
                                exit 1
                        fi
                        shift
                        normal="$1"
                        shift
                        ;;
                --urgent|-u)
                        if [ $# -lt 2 ] ; then
                                # some error message and exit
                                exit 1
                        fi
                        shift
                        urgent="$1"
                        shift
                        ;;
                --)     # end of options
                        shift
                        break
                        ;;
                -*)     # unknown option
                        # some error message
                        exit 1
                        ;;
                *)      # no option, maybe an argument
                        break
                        ;;
                esac
        done

        #
        # Read arguments, shift for every argument
        #
        # ...

        # final check
        if [ $# -gt 0 ] ; then
                # some error message and exit
                exit 1
        fi

/Thomas

 
 
 

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

Post by Tom Ca » Fri, 10 May 2002 06:46:59



> > For example I got to run like this:

> > do_sth.sh -NORMAL abc

> First some general remarks:

>    - There is no need to us a .sh suffix.

...

Thanks, Thomas.

But style is a personal matter.

In your sample codes, "--normal|-n)" seems to be the typo of
"-normal|-n)", isn't it?

 
 
 

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

Post by Tinti » Fri, 10 May 2002 07:40:25



Quote:

> In your sample codes, "--normal|-n)" seems to be the typo of
> "-normal|-n)", isn't it?

The standand GNU format for long options is to use the prefix --
 
 
 

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

Post by Tom Ca » Fri, 10 May 2002 10:17:02





> > In your sample codes, "--normal|-n)" seems to be the typo of
> > "-normal|-n)", isn't it?

> The standand GNU format for long options is to use the prefix --

Thanks. I am on HPUX and SUN OS, maybe that's why.
 
 
 

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

Post by those who know me have no need of my nam » Sun, 12 May 2002 03:26:20



Quote:>In your sample codes, "--normal|-n)" seems to be the typo of
>"-normal|-n)", isn't it?

thomas' sample was tied into his suggestion that you use `--' to prefix
options using words (so called `long' options).  since you don't want that
behavior you would do differently, e.g.,

while [ $# -gt 0 ]
do
  case $1 in
    -NORMAL) if [ $# -lt 2 ]
             then # error: no value
                  exit 1
             fi
             shift
             NORMAL="$1"
             shift
             ;;
    -URGENT) if [ $# -lt 2 ]
             then # error: no value
                  exit 1
             fi
             shift
             URGENT="$1"
             shift
             ;;
    # other options
    -*)      # error: unknown option
             exit 1
             ;;
    *)       # end of options
             break
             ;;
  esac
done

--
bringing you boring signatures for 17 years

 
 
 

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

Post by Thomas Weidenfell » Tue, 14 May 2002 16:36:03



> But style is a personal matter.

This is not a question of style. When in Rome, do what the Romans do.

Quote:> In your sample codes, "--normal|-n)" seems to be the typo of
> "-normal|-n)", isn't it?

No, you apparently didn't read my complete post. Here is the relevant
part again:

Quote:>>        - IMHO started by the GNU guys is is not uncommon to use a
>>          single '-' for one letter options (like '-u') and '--' for
>>          long options.

/Thomas