zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: zsh-workers@zsh.org
Subject: [RFC] zsh/getopx — zsh interface to getopt_long(3)
Date: Mon, 4 Dec 2017 21:24:32 -0600	[thread overview]
Message-ID: <2E5167F1-0116-474F-9039-9CD8E5BF6E9F@dana.is> (raw)

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



                 reply	other threads:[~2017-12-05  3:24 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2E5167F1-0116-474F-9039-9CD8E5BF6E9F@dana.is \
    --to=dana@dana.is \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).