LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   zparseopts - How to pass multiple argument to an option (https://www.linuxquestions.org/questions/linux-newbie-8/zparseopts-how-to-pass-multiple-argument-to-an-option-4175677149/)

blueray 06-16-2020 04:07 AM

zparseopts - If no argument is given to compulsory option, it takes next option as the argument
 
I am using zsh 5.4.2. The function that is causing issue is:

Code:

function zp () {
    zparseopts -E -watch:=o_watch -show=o_show
    echo "show            : $o_show"
    echo "watch          : $o_watch"
}

Output:

Code:

$ zp  --show --watch "Watching"
show            : --show
watch          : --watch Watching
   
$ zp  --watch --show
show            :
watch          : --watch --show

You can see that, If I do not pass a value to --watch (which's argument is mandatory) then it takes the next option in this case --show as the argument. It should actually show an error like zp:zparseopts:1: missing argument for option: -watch

Why is --watch taking --show as an argument instead of throwing an error.

blueray 06-16-2020 04:17 AM

zparseopts - How to make an option (not argument of the option) mandatory
 
In The example bellow:
Code:

function zp () {
    zparseopts -E -walk:=o_walk
    echo "walk: $o_walk"
}

I get the follwing output.
Code:

$ zp --walk "Walking"
walk            : --walk Walking
$ zp --walk
zp:zparseopts:2: missing argument for option: -walk
walk            :

Here the argument of the option is mandatory so I am getting this error.

How can I make the option mandatory so that I must pass --walk to zp else it will throw an error.

blueray 06-16-2020 04:35 AM

zparseopts - How to pass multiple argument to an option
 
In The example bellow:
Code:

function zp () {
    zparseopts -E -walk+:=o_walk
    echo "walk : $o_walk"
}

I get the following output:
Code:

$ zp --walk Walking --walk Run
walk : --walk Walking --walk Run
$ zp --walk "Walking Run"   
walk : --walk Walking Run

So, I have to use the option (--walk) multiple times to insert multiple argument (Walking / Run). Or, I can use double quotation mark to insert multiple argument using one option.

It looks weird if we do something like:
Code:

command -f "file1 file2 file3"
Is there any way I can insert multiple arguments in single option without using double quotation mark.

Something like:
Code:

command -f file1 file2 file3

pan64 06-16-2020 04:56 AM

you need to check getopt (and/or getopts) which is used to handle this.
Otherwise you need to check $# (and additionally $1, $2 ....)

see for example: https://dustymabe.com/2013/05/17/eas...a-bash-script/

this is a much better example: https://stackoverflow.com/questions/...d-line-options

pan64 06-16-2020 05:02 AM

duplicate of https://www.linuxquestions.org/quest...ry-4175677148/
please open only one thread about argument parsing

pan64 06-16-2020 05:02 AM

duplicate of https://www.linuxquestions.org/quest...ry-4175677148/
please open only one thread about argument parsing

blueray 06-16-2020 05:04 AM

What might be the solution with $# & $1, $2 ....?

pan64 06-16-2020 05:07 AM

as far as I see your problem is relatively complex, you need to look for a ready-made solution (like getopt) instead of re-implementing it again.

blueray 06-16-2020 05:08 AM

These are different questions regarding zparseopts. If I open one question with all these queries then no one will even touch these questions because it will be too complicated.

blueray 06-16-2020 05:09 AM

These are different questions regarding zparseopts. If I open one question with all these queries then no one will even touch these questions because it will be too complicated.

blueray 06-16-2020 05:11 AM

I have checked getopt, getopts & zparseopts. All my problems are solved with zparseopts except these three issues.

Moreover, I have to rewrite a lot of stuff if I try to replace zparseopts now.

blueray 06-16-2020 06:15 AM

The solution that I found is:
Code:

function zp () {
    if ! zparseopts -E -walk:=o_walk; then
        return 1
    fi
    if [ $#o_walk = 0 ]; then
        echo "required option --walk missing" >&2
        return 1
    fi
    echo "walk: $o_walk"
}


blueray 06-16-2020 06:19 AM

I actually realized that it looks perfectly natural to have -f "file1 file2 file3" and very weird to have -f file1 file2 file3. So, I am quiting the pursuit to the answer of this question.


All times are GMT -5. The time now is 06:25 AM.