On Tue, Nov 18, 2014 at 01:51:53PM +0800, Clark Wang wrote:
> [STEP 101] $ cat foo.ksh
> optstr="[-][=100:color]:?[COLOR:=auto]{[auto?Auto][never?Never][always?Always]}"
>[...]
> The foo.ksh --man output indicates that it should support --color=always
> but the foo.ksh --color=always reported error. Any idea?

I don't even pretend to have authoritative knowledge of getopts :-) but
I've found that the suboption parsing really only works right if you
give it both flag and long option names.  I've never had much success
when I try to use =100 value assignments like you do, so I just use a
nested case instead; it fits the pattern of option parsing, and doesn't
surprise co-workers.

So, working your example, I think I'd wind up with something like
this, based on similar stuff that works in some of our code.

        #!/usr/bin/env ksh

        compound opts=( color=auto; other=defaults )

        usage=$'[-1s1?1][+NAME?example - of getopts]
        [C:color?Blah blah blah.]:[val:='"${opts.color//:/::}"$']{
          [a:auto?Use color if standard input is a terminal.]
          [A:always?Always use color attributes.]
          [n:never?Never use color attributes.]}
        \nfile [...]\n
        [+MORE?Stuff.]'

        opt=
        while   getopts -a "${0##*/}" "$usage" opt
        do      case    "$opt" in
                C)      case    "$OPTARG" in
                        a)      opts.color=auto ;;
                        A)      opts.color=always ;;
                        n)      opts.color=never ;;
                        esac ;;
                esac
        done

        typeset -p opts
        exit 0

Cheers,
Bob

-- 
Bob Krzaczek, Chester F. Carlson Center for Imaging Science, RIT
phone +1-585-4757196, email [email protected], icbm 43.08586N 77.67744W

Attachment: pgpj8ytPLEV4o.pgp
Description: PGP signature

_______________________________________________
ast-users mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-users

Reply via email to