Permalink
Browse files

initial

  • Loading branch information...
0 parents commit ed0bc8cf3046925cd88e93794db706b9899b7561 @dominictarr committed Dec 7, 2011
@@ -0,0 +1,3 @@
+node_modules
+node_modules/*
+npm_debug.log
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+
+#source optimist.sh into your bash script
+#
+. optimist.sh
+. <(argv "$@")
+
+for key in ${!argv_*}; do
+ value="${!key}"
+ echo "$key"="$value"
+done
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+
+PREFIX=${PREFIX:-argv_}
+
+argument () {
+ if [ $STATE == ARG ]; then echo -n "$PREFIX"ARG_"$ARG"; let ARG=$ARG+1; fi
+ echo =\""$1"\"
+ STATE=ARG
+}
+argv () {
+ echo '#!/usr/bin/env bash'
+ STATE=ARG
+ ARG=0
+ while true; do
+
+ case "$1" in
+ --)
+ if [ $STATE == VAL ]; then echo =\"true\"; fi
+ STATE=ARG
+ shift
+ for a in "$@"; do
+ argument $a
+ done
+ return 0
+ ;;
+ --no-*)
+ if [ $STATE == VAL ]; then echo =\"true\"; fi
+ echo "$PREFIX""${1##--no-}"=\"false\"
+ STATE=ARG
+ ;;
+ -*=*)
+ if [ $STATE == VAL ]; then echo =\"true\"; fi
+ echo $PREFIX`expr match "$1" '--*\(.*\)='`=\"`expr match "$1" '--*.*=\(.*\)$'`\"
+ STATE=ARG
+ ;;
+ -*)
+ if [ $STATE == VAL ]; then echo =\"true\"; fi
+ echo -n $PREFIX`expr match "$1" '--*\(.*$\)'`
+ STATE=VAL
+ ;;
+ *)
+ argument "$1"
+ ;;
+ esac
+ shift
+ if [ "x$1" == x ]; then break; fi
+ done
+
+ if [ $STATE == VAL ]; then echo =\"true\"; fi
+
+}
+
+demand () {
+PREFIX=demand_ parseargs "$@"
+ . <(PREFIX=demand_ parseargs "$@")
+ #echo $demand_ARG_0
+ if [ x$demand_ARG_0 == x ]; then
+ echo $demand_ARG_1 >&2
+ exit 1
+ fi
+}
+
+
+if [ $0 == $BASH_SOURCE ]; then parseargs $@; fi
@@ -0,0 +1,11 @@
+{ "name": "optimist.sh"
+, "version": "0.0.0"
+, "description": ""
+, "homepage": "http://github.com/dominictarr/optimist.sh"
+, "repository":
+ { "type": "git"
+ , "url": "https://github.com/dominictarr/optimist.sh.git" }
+, "dependencies": {}
+, "devDependencies": {}
+, "author": "Dominic Tarr <dominic.tarr@gmail.com> (http://bit.ly/dominictarr)"
+, "scripts": { "test": "meta-test test/*.js" } }
@@ -0,0 +1,36 @@
+# optimist.sh
+
+behaves just like substack/optimist but is for bash instead of nodejs
+
+```
+#source optimist.sh into your bash script
+. optimist.sh
+```
+
+source the output back into this script.
+it will create env vars with the prefix `argv_`
+
+optionally set a different prefix `. <(PREFIX whatever_ argv "$@")`
+
+```
+. <(argv "$@")
+
+# iterate over them like this:
+
+for key in ${!argv_*}; do
+ value="${!key}"
+ echo "$key"="$value"
+done
+```
+
+supports a bunch of arguments
+
+```
+./example.sh arg1 --key1 value1 --key2=value2 --no-falsey --truthy -t -f -- arg2 arg3
+```
+
+##tests
+
+```
+./test/parse_test
+```
@@ -0,0 +1,7 @@
+#!/usr/bin/env bash
+test6_boolean1="true"
+test6_boolean2="false"
+test6_ARG_0="a1"
+test6_ARG_1="b2"
+test6_key1="val1"
+test6_key2="val2"
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+test3_boolean_false="false"
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+test2_boolean_true="true"
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+test5_key="val"
@@ -0,0 +1,4 @@
+#!/usr/bin/env bash
+test4_ARG_0="a"
+test4_ARG_1="b"
+test4_ARG_2="c"
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+test1_key="val"
@@ -0,0 +1,2 @@
+#!/usr/bin/env bash
+test7_values="a b c"
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+#. ../parseargs
+
+set -e
+
+TEST=0
+FAILURES=0
+echo 1..7
+
+cd `dirname $BASH_SOURCE`
+
+. ../optimist.sh ## must use parse args like this, if you want spaces to come through properly
+
+testparse () {
+
+ let TEST=$TEST+1
+ file=$1
+ shift
+
+ diff <(PREFIX=test"$TEST"_ argv "$@") $file > /tmp/parse_args.err && ret=0 || ret=1
+
+ if [ $ret == '0' ]; then
+ echo ok -- $TEST $file
+ else
+ echo not ok -- $test $file
+ cat /tmp/parse_args.err
+ let FAILURES=$FAILURES+1
+ fi
+}
+
+testparse examples/simple.args --key val
+testparse examples/boolean_true.args --boolean_true
+testparse examples/boolean_false.args --no-boolean_false
+testparse examples/positional.args a b c
+testparse examples/inline.args --key=val
+testparse examples/all.args --boolean1 --no-boolean2 a1 b2 --key1 val1 --key2 val2
+testparse examples/with-spaces.args --values "a b c"
+
+echo end
+
+case $FAILURES in
+ 0) echo $TEST passed ;;
+ *) echo $FAILURES failed ;;
+esac
+
+exit $FAILURES

0 comments on commit ed0bc8c

Please sign in to comment.