Skip to content

Instantly share code, notes, and snippets.

@kolodny
Last active November 25, 2020 08:00

Revisions

  1. kolodny revised this gist Nov 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion args.sh
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ function parse_args() {
    local value="${arg#*=}"
    eval $key=\$value
    ;;
    --*)
    --?*)
    local key=${arg#--}
    if [ ! ${!key+x} ]; then
    echo "invalid option -- $key" 1>&2
  2. kolodny revised this gist Nov 18, 2016. 1 changed file with 37 additions and 11 deletions.
    48 changes: 37 additions & 11 deletions args.sh
    Original file line number Diff line number Diff line change
    @@ -1,25 +1,51 @@
    #!/bin/bash
    #!/bin/sh

    set -euo pipefail

    #declare valid arguments here
    bar=
    baz=

    function parse_args() {
    args=()
    for (( i=1; i<=$#; i++ )); do
    local i=1
    while [ $i -le $# ]; do
    local arg="${!i}"
    case "${arg}" in
    --*=*)
    arg="${!i}"
    arg=${arg#--}
    readonly "${arg%=*}"="${arg#*=}"
    continue
    local key="${arg%=*}"
    if [ ! ${!key+x} ]; then
    echo "invalid option -- $key" 1>&2
    exit 2
    fi
    local value="${arg#*=}"
    eval $key=\$value
    ;;
    --*)
    ((i++))
    arg=${arg#--}
    readonly "${arg}"="${!i}"
    continue
    local key=${arg#--}
    if [ ! ${!key+x} ]; then
    echo "invalid option -- $key" 1>&2
    exit 2
    fi
    if [ $i -ge $# ]; then
    echo "option requires an argument -- $key" 1>&2
    exit 2
    fi
    let "i++"
    local value=${!i}
    eval $key=\$value
    ;;
    *)
    args+=("$arg")
    esac
    done
    let "i++"
    done
    }
    parse_args "$@"
    parse_args "$@"

    echo "bar is $bar"
    echo "baz is $baz"
    for arg in "${args[@]}"; do
    echo "arg is $arg"
    done
  3. kolodny revised this gist Nov 17, 2016. 1 changed file with 23 additions and 24 deletions.
    47 changes: 23 additions & 24 deletions args.sh
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,25 @@
    #!/bin/bash

    args=()
    for (( i=1; i<=$#; i++ )); do
    arg="${!i}"
    case "${arg}" in
    --*=*)
    arg="${!i}"
    arg=${arg#--}
    declare "${arg%=*}"="${arg#*=}"
    continue
    ;;
    --*)
    ((i++))
    declare "${arg}"="${!i}"
    continue
    ;;
    *)
    args+=("$arg")
    esac

    done

    for arg in "${args[@]}"; do
    echo "arg is $arg"
    done
    function parse_args() {
    args=()
    for (( i=1; i<=$#; i++ )); do
    local arg="${!i}"
    case "${arg}" in
    --*=*)
    arg="${!i}"
    arg=${arg#--}
    readonly "${arg%=*}"="${arg#*=}"
    continue
    ;;
    --*)
    ((i++))
    arg=${arg#--}
    readonly "${arg}"="${!i}"
    continue
    ;;
    *)
    args+=("$arg")
    esac
    done
    }
    parse_args "$@"
  4. kolodny revised this gist Nov 17, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion args.sh
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    args=()
    for (( i=1; i<=$#; i++ )); do
    arg="${!i}"
    # echo "about to examine $arg"
    case "${arg}" in
    --*=*)
    arg="${!i}"
  5. kolodny created this gist Nov 17, 2016.
    27 changes: 27 additions & 0 deletions args.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/bin/bash

    args=()
    for (( i=1; i<=$#; i++ )); do
    arg="${!i}"
    # echo "about to examine $arg"
    case "${arg}" in
    --*=*)
    arg="${!i}"
    arg=${arg#--}
    declare "${arg%=*}"="${arg#*=}"
    continue
    ;;
    --*)
    ((i++))
    declare "${arg}"="${!i}"
    continue
    ;;
    *)
    args+=("$arg")
    esac

    done

    for arg in "${args[@]}"; do
    echo "arg is $arg"
    done