LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2020, 04:07 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Rep: Reputation: 2
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.
 
Old 06-16-2020, 04:17 AM   #2
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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.
 
Old 06-16-2020, 04:35 AM   #3
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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
 
Old 06-16-2020, 04:56 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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

Last edited by pan64; 06-16-2020 at 05:06 AM.
 
1 members found this post helpful.
Old 06-16-2020, 05:02 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
duplicate of https://www.linuxquestions.org/quest...ry-4175677148/
please open only one thread about argument parsing
 
Old 06-16-2020, 05:02 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
duplicate of https://www.linuxquestions.org/quest...ry-4175677148/
please open only one thread about argument parsing
 
Old 06-16-2020, 05:04 AM   #7
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
What might be the solution with $# & $1, $2 ....?
 
Old 06-16-2020, 05:07 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,910

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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.
 
Old 06-16-2020, 05:08 AM   #9
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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.
 
Old 06-16-2020, 05:09 AM   #10
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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.
 
Old 06-16-2020, 05:11 AM   #11
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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.

Last edited by blueray; 06-16-2020 at 05:16 AM.
 
Old 06-16-2020, 06:15 AM   #12
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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"
}
 
Old 06-16-2020, 06:19 AM   #13
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
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.
 
  


Reply

Tags
zsh



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] iptables bad argument Bad argument `5060' treschen Linux - Newbie 3 11-10-2012 09:29 AM
[SOLVED] shell scripting: value of argument dissapears depending on argument order akelder Programming 5 03-21-2011 11:27 PM
message sending failed : Error[22 ] invalid argument .....but each and every argument rakeshranjanjha Linux - Software 2 01-07-2008 11:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration