help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Handling short and long options


From: Alex fxmbsw7 Ratchev
Subject: Re: Handling short and long options
Date: Tue, 7 Sep 2021 05:14:15 +0200

well imho such is weak

at a standart minimum, a -o or --opt with a one required argument, needs
still to be parsed both ways
i mean with space for next arg or a = for inside_this_arg
.. not just the --opt variant

the code to support this could be simple
but i discourage the use of mixed args ( eg with an equal sign instead of
on its own )

codewise, per !i in args, parse off the part before = whenever exisiting,
and check then if its a supported arg, then check whenever that arg needs
required number of args, then parse em, the first guess being the part
after =, in case

like without = an arg can have 3 args, no =, -o 1 2 3 otheropt, --opt 1 2 3
otheropt

On Tue, Sep 7, 2021, 04:55 angioberlinguer <angioberlinguer@protonmail.com>
wrote:

> Hi Alex , thanks fuck for that!  It's not a total disaster then.  Have
> found some useful discussions
> in the following stackoverflow question.  They handle short-options with
> spaces and long-options with
> equal signs.
>
>
> https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/29754866#29754866
>
>
>
>
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> On Tuesday, September 7, 2021 2:10 AM, Alex fxmbsw7 Ratchev <
> fxmbsw7@gmail.com> wrote:
>
> > i have for args around the following system
> > touch assoc elements aka supported args on it with some features like
> > argument count taking, and then running the args thru such a parser
> >
> > btw its not the gnu guys, .. i guess..
> >
> > On Tue, Sep 7, 2021, 01:13 angioberlinguer
> angioberlinguer@protonmail.com
> > wrote:
> >
> > > It is a way to handle short and long options manually. Pelhaps I can
> > > simplify the "-s"
> > > option. For the "-s" option, I wanted to accept wildcards (e.g. A*)
> which
> > > the shell would
> > > expand.
> > > Noticed a small mistake in the last part, which should be
> > > (*) fls+=( "$2" ) ; shift 1 ;;
> > > esac
> > > ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> > > On Monday, September 6, 2021 11:05 PM, Dennis Williamson <
> > > dennistwilliamson@gmail.com> wrote:
> > >
> > > > On Mon, Sep 6, 2021, 5:40 PM angioberlinguer via help-bash@gnu.org
> > > > wrote:
> > > >
> > > > > I have made the following function to parse short and long options.
> > > > > Perhaps I do not need to
> > > > > call `set -- $*`.
> > > > > The while loop could also be changed. I want to allow splitting on
> > > > > space
> > > >
> > > > > and equal sign
> > > > > so I can call it with `-s val` and `--src=val`.
> > > > > rando ()
> > > > > {
> > > > > local incl=() fls=()
> > > > > IFSPREV="$IFS" # Save IFS (splits arguments on whitespace by
> default)
> > > > > IFS=" =" # Split arguments on " " and "="
> > > > > set -- $* # Set positional parameters to command line arguments
> > > > > IFS="$IFSPREV" # Set original IFS
> > > > > local iarg=0 narg="$#"
> > > > > while (( narg > 0 )); do
> > > > > opt="$1"
> > > > > iarg=$(( iarg + 1 ))
> > > > > case $opt in
> > > > > ("--incl") incl+=("$2") ; shift 2 ;;
> > > > > ("-s"|"--src"|"--source") src=$( "$2" ) ; shift 2 ;;
> > > > > ("-d"|"--dst"|"--destin") dst="$2" ; shift 2 ;;
> > > > > ("--") shift 1 ; break ;;
> > > > > ("-") printf '%s\n' "Unknown option: $1" ; shift 1 ;;
> > > > > () fls+=( "$2" ) ; shift 1 ;;esac
> > > > > done
> > > >
> > > > WTH is going on?
>
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]