LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-29-2021, 06:42 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Handling long options with getopts


I am parsing options with `getopts` but would like to handle long options as well.

Code:
mygetopts ()
{
 aggr=()
 for arg in "$@"; do
   case $arg in
    ("--context")  aggr+=( "-C" ) ;;
    (*)  aggr+=( "$arg" ) ;;
   esac
 done

 set -- "${aggr[@]}"
 
 local OPTIND OPTARG
 local shortopts="C:"
 while getopts "$shortopts" arg; do
   case $arg in
    ("C") context="$OPTARG" ;;
    (*) break ;; 
   esac
 done
}
But I wonder whether the use of set -- "${aggr[@]}" is correct.

Or is the following (using `eval`) more appropriate?

Code:
eval set -- "${aggr[@]}"
or

Code:
eval "set -- ${aggr[@]}"

Last edited by Faki; 10-30-2021 at 02:46 AM.
 
Old 10-30-2021, 03:53 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
https://stackoverflow.com/questions/...ptions/7948533
 
Old 10-30-2021, 09:09 AM   #3
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,627

Rep: Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556Reputation: 2556
Quote:
Originally Posted by Faki View Post
But I wonder whether the use of set -- "${aggr[@]}" is correct.

Or is the following (using `eval`) more appropriate?

Code:
eval set -- "${aggr[@]}"
or

Code:
eval "set -- ${aggr[@]}"
1. Not sure how this is different to what you've asked already...

https://www.linuxquestions.org/questions/linux-desktop-74/alternative-to-eval-4175702722
https://www.linuxquestions.org/questions/linux-software-2/difference-between-%60eval-set-%24opts-%60-and-the-direct-call-%60set-%24opts-%60-4175702775


2. Use ShellCheck to check shell scripts.

The warnings/errors it raises provide your answer.

 
Old 10-30-2021, 10:56 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
Quote:
Originally Posted by boughtonp View Post
1. Not sure how this is different to what you've asked already...
this eval set construct is completely pointless with arrays. That can only cause additional troubles
the goal of this eval set is to parse/split the string into an array.
 
Old 10-31-2021, 07:14 AM   #5
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
Yeah, you can trick getopts into doing it with something like this:
Code:
while getopts ':a:b:v-:' opt
do
  case "$opt" in
    a)  of_a=1 ; oarg_a=$OPTARG  ;;
    b)  of_b=1 ; oarg_b=$OPTARG  ;;
    v)  of_v=1 ;;
    -)  case "$OPTARG" in
          long-opt) of_long_opt=1 ;;
        esac
        ;;
    \?) echo "Unexpected option: -$OPTARG" 
	exit 1 ;;
    :)  echo "Missing operand for option -$OPTARG" 
	exit 1 ;;
  esac
done
.. but it's very fragile and I wouldn't recommend it.


If you want long-options and portability is not a concern then stick with gnu getopt.

If portability is a concern then you'll most likely want to stick with the POSIX rules: short-options only, and no optional option arguments, in which case getopts is a good choice.
 
Old 10-31-2021, 09:06 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,039

Rep: Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347Reputation: 7347
you don't need getopt at all, you can simply write a while loop to process options. That is not that difficult:
https://stackoverflow.com/questions/...uments-in-bash

But obviously you can have different needs, in that case probably better use another language. bash is not really suitable to do fancy things like parsing complex argument structures.
 
Old 10-31-2021, 09:44 AM   #7
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,918

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
Yes, it's pretty easy so long as you don't support POSIX option combining.

I have, in the past, as an exercise, written a bash script that does its own parsing and which supports combined option strings, but I wouldn't want to use that in something real, unless there was no other choice.

There's a reason why getopt/getops exist.
 
  


Reply

Tags
bash, eval, getopts, set



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
How Does getopts Work with Combined Multiple Options? ghborrmann Linux - Software 3 12-26-2013 03:12 PM
getopts long word options maddyfreaks Linux - Newbie 6 06-07-2012 06:21 AM
[SOLVED] getopts is not considering (skipping) one of the options akhand jyoti Linux - Newbie 3 11-18-2011 01:44 PM
long long long: Too long for GCC Kenny_Strawn Programming 5 09-18-2010 01:14 AM
Passing command line arguments through getopts and long options neville310 Programming 3 04-16-2007 06:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:02 AM.

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