Coder Social home page Coder Social logo

pure-getopt's Introduction

pure-getopt

Build Status

pure-getopt is a drop-in replacement for GNU getopt, implemented in pure Bash compatible back to 2.05b. It makes no external calls and faithfully reimplements GNU getopt features, including:

  • all three calling forms in the synopsis
  • all getopt options
  • matching error messages
  • matching return codes
  • proper handling of abbreviated long options
  • alternative parsing mode (long options with single dash)
  • GETOPT_COMPATIBLE flag
  • POSIXLY_CORRECT flag
  • leading + or - on options string

How to use it

pure-getopt provides a single bash function getopt that you can insert directly into your script. It should be defined before calling getopt from your code, so either place the definition above your code, or use this pattern (recommended):

#!/bin/bash

main() {
    declare argv
    argv=$(getopt -o fb: --long foo,bar: -- "$@") || return
    eval "set -- $argv"

    declare foo=false bar=

    while true; do
        case $1 in
            f|foo) foo=true ; shift ;;
            b|bar) bar=$2 ; shift 2 ;;
            --) break ;;
        esac
    done

    # etc
}

# INSERT getopt function here
getopt() {
    ...
}

# CALL main at very bottom, passing script args.
# The test here distinguishes script execution from "source myscript.bash" which
# will define the functions without calling main, for calling functions from
# another script or testing at the command-line.
[[ $BASH_SOURCE != "$0" ]] || main "$@"

Differences between pure-getopt and GNU getopt

The only intentional divergences between pure-getopt and GNU getopt are either inconsequential or due to bugs in GNU getopt:

  1. GNU getopt mishandles ambiguities in abbreviated long options, for example this doesn't produce an error message:

    getopt -o '' --long xy,xz -- --x
    

    but this does produce an error message:

    getopt -o '' --long xy,xz: -- --x
    

    Pure-getopt generates an error message in both cases, diverging from GNU getopt to fix this bug.

  2. In the case of an ambiguous long option with an argument, GNU getopt generates an error message that includes the argument:

    getopt: option '--x=foo' is ambiguous; possibilities: '--xy' '--xz'
    

    We consider this a bug in GNU getopt, since the value might be very long and inappropriate for printing to the screen, and since GNU getopt ordinarily omits the value in its error messages. Pure-getopt's error message in this case is:

    getopt: option '--x' is ambiguous; possibilities: '--xy' '--xz'
    
  3. Pure-getopt uses a different method of quoting the output. The result is the same as GNU getopt when eval'd by the shell.

References

pure-getopt's People

Contributors

agriffis avatar itspriddle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pure-getopt's Issues

Failure parsing long option

Hi,

I'm seeing a problem handling long options with your getopt function.

I've created the following test case: https://gist.github.com/robinbowes/e3e656736f0421985173091b41f7998b

My goal is to handle an options value containing another option like this: test.sh --extra "--foo=bar" but I'm finding that I can't even parse a simple option like this: test.sh --extra foo.

This is the output from my test script:

$ ./test.sh --extra foo
[ '--extra' 'foo' '--']
$@: --extra foo --
argv: --extra
$1: --extra
$2: foo
$@: --
argv: foo
$1: --
$2: undef
Something went wrong processing command line options

It seems that foo is being treated as another option rather than being the value for the --extra option.

Am I doing something wrong here, or is this a bug?

test suite returns zero status even when failing

test 50 is currently failing, but the script returns zero status anyway

$ ./test.bash
...
 50. Testing --help FAIL
--- reference
+++ mine
@@ -2,24 +2,19 @@
 OUT: 
 ERR: 
 Usage:
- getopt <optstring> <parameters>
- getopt [options] [--] <optstring> <parameters>
- getopt [options] -o|--options <optstring> [options] [--] <parameters>
-
-Parse command options.
+ getopt optstring parameters
+ getopt [options] [--] optstring parameters
+ getopt [options] -o|--options optstring [options] [--] parameters
 
 Options:
- -a, --alternative             allow long options starting with single -
- -l, --longoptions <longopts>  the long options to be recognized
- -n, --name <progname>         the name under which errors are reported
- -o, --options <optstring>     the short options to be recognized
- -q, --quiet                   disable error reporting by getopt(3)
- -Q, --quiet-output            no normal output
- -s, --shell <shell>           set quoting conventions to those of <shell>
- -T, --test                    test for getopt(1) version
- -u, --unquoted                do not quote the output
-
- -h, --help     display this help and exit
- -V, --version  output version information and exit
-
-For more details see getopt(1).
+ -a, --alternative            Allow long options starting with single -
+ -h, --help                   This small usage guide
+ -l, --longoptions <longopts> Long options to be recognized
+ -n, --name <progname>        The name under which errors are reported
+ -o, --options <optstring>    Short options to be recognized
+ -q, --quiet                  Disable error reporting by getopt(3)
+ -Q, --quiet-output           No normal output
+ -s, --shell <shell>          Set shell quoting conventions
+ -T, --test                   Test for getopt(1) version
+ -u, --unquote                Do not quote the output
+ -V, --version                Output version information

Getopt version with -T

 51. Testing -T PASS
 52. Testing GETOPT_COMPATIBLE -T PASS
 53. Testing GETOPT_COMPATIBLE -T PASS

Setting shell with -s

 54. Testing -s sh -o xy:z:: --long=abc,def:,dez:: -- -x -y a\b c PASS
 55. Testing -s bash -o xy:z:: --long=abc,def:,dez:: -- -x -y a\b c PASS
 56. Testing -s foo -o xy:z:: --long=abc,def:,dez:: -- -x -y a\b c PASS

$ echo $?
0

tests failing for alternative mode

Some of the tests are failing:

$ ./test.bash | grep FAIL
 31. Testing -a -o xy:z:: --long=abc,def:,dez:: -- -xyz -def FAIL
 41. Testing -a -o xy:z:: --long=abc,def:,dez:: -- -de FAIL
 42. Testing -a -o xy:z:: --long=abc,def:,dez:: -- -de=foo FAIL
 43. Testing -a -o xy:z:: --long=abc,def:,dez:: -- -de foo FAIL

These are tests for alternative mode. They're failing because getopt reports the error with a single dash, but pure-getopt reports with two dashes:

$ ./test.bash 31
 31. Testing -a -o xy:z:: --long=abc,def:,dez:: -- -xyz -def FAIL
--- reference
+++ mine
@@ -1,3 +1,3 @@
 EXIT: 1
-OUT:  -x -y 'z' --
-ERR: getopt: option '-def' requires an argument
+OUT:  '-x' '-y' 'z' '--'
+ERR: getopt: option '--def' requires an argument

Not sure when this discrepancy crept in. Looking back at old releases of util-linux, this behavior seems to have been consistent for a long time. Neither has pure-getopt changed since the test suite was passing.

GPL 3.0 Alternative

I'd like to use this project for command line parsing in my .dotfiles, but I don't want to write free-as-in-beards software, so I'd appreciate LGPL at least. I know the arguments for GPL, they are great, but I find that using GPL just causes me to spend more time answering license questions than collaborating.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.