zsh-workers
 help / color / mirror / code / Atom feed
* [RFC] zsh/getopx — zsh interface to getopt_long(3)
@ 2017-12-05  3:24 dana
  0 siblings, 0 replies; only message in thread
From: dana @ 2017-12-05  3:24 UTC (permalink / raw)
  To: zsh-workers

Hey there,

In my 'consumer' shell scripts i often use the util-linux version of getopt(1),
which provides many functionality and ease-of-use benefits compared to the BSD
getopt(1), the getopts built-in, and the zparseopts built-in, by way of
getopt_long(3). The major down side to this utility is that it's rather hard to
come by on non-GNU/Linux systems.

Since getopt_long(3) itself has been portable across all of the major UNIX-like
systems for about 15 years now, it seemed like subsuming that functionality into
zsh via a module might be a decent idea. So i've done that, or at least i've got
a working prototype:

https://github.com/okdana/zsh/blob/dana/getoptx/Src/Modules/getoptx.c

I'm not a fantastic C programmer so there's probably a lot of weird stuff like
questionable allocations and temp variables, the code doesn't conform to the zsh
style guide lines, and i'm not super married to either the name or the API — but
other than that it seems to do the job. Here's a partial usage example
illustrating the current API (including the -c option, which makes it easier to
support use cases like `tail -15` by leaving adjacent numeric options grouped
together):

  getoptx -cEA argv -l help,lines:,version hn:V || return 1

  while (( $# )); do
    case $1 in
      -[0-9]##)     lines=${1#-}; shift ;;
      -h|--help)    : print help; return 0 ;;
      -n|--lines)   : validate $2 somehow; lines=$2; shift 2 ;;
      -V|--version) : print version; return 0 ;;
      --)           shift; break ;; # any remaining argv are operands
    esac
  done

Do you reckon this is something that might be suitable for inclusion into the
main project (assuming i get it cleaned up)? Or is the arg-parsing 'market' too
saturated already with the alternatives i mentioned?

(Of course, any other thoughts you might have would be appreciated too.)

cheers

dana



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2017-12-05  3:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-05  3:24 [RFC] zsh/getopx — zsh interface to getopt_long(3) dana

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).