Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm looking for way to process shell script arguments that is cleaner and more "self documenting" than getopt/getopts.

It would need to provide...

  • Full support of long options with or without a value after '=' or ' '(space).
  • Support of short options including concatenated options (-a -b -c / -abc).
  • No need to support single hyphen long options (i.e. find -name ...)
  • Proper handling of hyphenated option names (i.e. --ignore-case)
  • Proper handling of quoted option values (i.e. --text "A text string")

I would like to eliminate the overhead of the big loop with embedded case statement that getopt/getopts requires and reduce option processing to something like...

option=argumentparse "$@"
[[ option == ""           ]] && helpShow
[[ option =~ -h|--help    ]] && helpShow
[[ option =~ -v|--version ]] && versionShow
[[ option =~ -G|--GUI     ]] && GUI=$TRUE
[[ option =~ --title      ]] && TITLE=${option["--title"]}

Here, an argumentparse() function resolves the various syntax possibilities into a consistent format, perhaps an associative array.

Awk looks like a good candidate for implementing this but have been unable to find a solution, in awk or any other language, that meets these requirements.

There must be something coded out there somewhere. Any ideas?

share|improve this question
 
Have you looked at shflags? –  rush Feb 18 at 9:15
 
Thanks for the info! Just checked it out. Some interesting things there but still pretty involved. –  DocSalvage Feb 18 at 10:07
 
I've used a few different solutions, but eventually just settled for getopts. I had to accept it's limitations, but it's a POSIX shell standard, so it felt like a worthwhile trade off. I also avoid using shell loops, and agree they are generally inefficient, but I just bit the bullet for getopts. I still might be able to point you in a solid direction. What are your goals? Efficiency? Portability? Features? Simplicity? Which are your willing to compromise, to achieve the others? –  TechZilla Feb 18 at 21:00
 
5min limit so had to add new comment... Thank you for jumping in! The most important goal would be simplicity/clarity of code using the solution. The function(s) could be fairly complex (use awk, sed, regex's, etc.) if they are "black boxes" that make the calling code easier to work with. Next would be "quality" of the function code which I define as a combination of consistency, symmetry and what we used to call "elegance". Third, would be efficiency, followed by features and portability to shells other than bash. –  DocSalvage Feb 18 at 21:36
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.