Eric Blake
2018-Jan-24 03:58 UTC
[Libguestfs] [nbdkit PATCH] maint: Improve ./nbdkit handling of --opt=value
Although useful for in-tree debugging, the ./nbdkit script was not very nice when it comes to using the '--longopt=value' single-string form of options, compared to the two-string '--longopt value' form. Since we used a glob for the multiple spellings of --exportname and --pidfile, the script would try to treat the next argument literally instead of recognizing that the argument was already consumed. Likewise, the script was not recognizing '--filter=foo'. Since the script already relies on bash, we can exploit bash's case fallthrough (using ';&' instead of ';;'), to rewrite inline --filter=foo without having to duplicate logic. This does NOT try to honor option abbreviations. If you are testing that aspect of getopt_long, don't use this script ;) Signed-off-by: Eric Blake <eblake@redhat.com> --- I'm pushing this one now, as it tripped me up during testing. nbdkit.in | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/nbdkit.in b/nbdkit.in index d1caf57..996616d 100644 --- a/nbdkit.in +++ b/nbdkit.in @@ -65,6 +65,11 @@ verbose while [ $# -gt 0 ]; do case "$1" in # Flags that take an argument. We must not rewrite the argument. + # But make sure globs don't mishandle longopts with =. + --export*=* | --pid*=*) + args[$i]="$1" + shift + ;; -e | --export* | -g | --group | -i | --ip* | -P | --pid* | -p | --port | --run | --selinux-label | -t | --threads | --tls | --tls-certificates | -U | --unix | -u | --user) args[$i]="$1" ((++i)) @@ -80,6 +85,11 @@ while [ $# -gt 0 ]; do ;; # Filters can be rewritten if purely alphanumeric. + --filter=*) + tmp=${1#*=} + shift + set - --filter "$tmp" "$@" + ;& # fallthru --filter) args[$i]="--filter" ((++i)) -- 2.14.3
Richard W.M. Jones
2018-Jan-24 14:16 UTC
Re: [Libguestfs] [nbdkit PATCH] maint: Improve ./nbdkit handling of --opt=value
On Tue, Jan 23, 2018 at 09:58:27PM -0600, Eric Blake wrote:> Although useful for in-tree debugging, the ./nbdkit script was not > very nice when it comes to using the '--longopt=value' single-string > form of options, compared to the two-string '--longopt value' form. > Since we used a glob for the multiple spellings of --exportname and > --pidfile, the script would try to treat the next argument literally > instead of recognizing that the argument was already consumed. > Likewise, the script was not recognizing '--filter=foo'.Right, this is the reason why all the tests use ‘--filter foo’, which is annoying and awkward.> Since the script already relies on bash, we can exploit bash's > case fallthrough (using ';&' instead of ';;'), to rewrite inline > --filter=foo without having to duplicate logic.Nice, I learn a new thing every day ...> This does NOT try to honor option abbreviations. If you are testing > that aspect of getopt_long, don't use this script ;) > > Signed-off-by: Eric Blake <eblake@redhat.com> > --- > > I'm pushing this one now, as it tripped me up during testing. > > nbdkit.in | 10 ++++++++++ > 1 file changed, 10 insertions(+) > > diff --git a/nbdkit.in b/nbdkit.in > index d1caf57..996616d 100644 > --- a/nbdkit.in > +++ b/nbdkit.in > @@ -65,6 +65,11 @@ verbose> while [ $# -gt 0 ]; do > case "$1" in > # Flags that take an argument. We must not rewrite the argument. > + # But make sure globs don't mishandle longopts with =. > + --export*=* | --pid*=*) > + args[$i]="$1" > + shift > + ;; > -e | --export* | -g | --group | -i | --ip* | -P | --pid* | -p | --port | --run | --selinux-label | -t | --threads | --tls | --tls-certificates | -U | --unix | -u | --user) > args[$i]="$1" > ((++i)) > @@ -80,6 +85,11 @@ while [ $# -gt 0 ]; do > ;; > > # Filters can be rewritten if purely alphanumeric. > + --filter=*) > + tmp=${1#*=} > + shift > + set - --filter "$tmp" "$@" > + ;& # fallthru > --filter) > args[$i]="--filter" > ((++i))Yup, this is great, ACK. You might also want to adjust the tests to use ‘--filter=foo’ (no need to review that change). Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Seemingly Similar Threads
- [nbdkit PATCH v2 1/5] maint: Improve ./nbdkit option parsing
- Pacaging/build issues with AIX and vac (dovecot-2.2.25)
- Pacaging/build issues with AIX and vac (dovecot-2.2.25)
- [nbdkit PATCH v2 2/2] utils: Revamp nbdkit_parse_size
- [RFC nbdkit PATCH] utils: Revamp nbdkit_parse_size