nk412 / optparse

Simple command line arguments parser for BASH
MIT License
158 stars 32 forks source link

Add handling of "--" #10

Open K-Ko opened 10 years ago

K-Ko commented 10 years ago

Test script:

#!/bin/bash
. optparse.bash
# Test is used as flag (0|1)
optparse.define short=t long=test desc='Test run' variable=TEST value=1 default=0
optparse.define short=n long=nice desc='Nice level' variable=NICE default=10
# additional arguments for usage()
ARGUMENTS="-- arg1 arg2 ..."
source $( optparse.build )
echo "Test: $TEST"
echo "Nice: $NICE"
echo "Remaining arguments: $@"

Results:

# ./t.sh -h
usage: ./t.sh [OPTIONS] -- arg1 arg2 ...

OPTIONS:

        -t --test:                    Test run [flag]
        -n --nice:                    Nice level [default:10]
        -? -h --help:                 usage

# ./t.sh
Test: 0
Nice: 10
Remaining arguments: 

# ./t.sh --test -n 5
Test: 1
Nice: 5
Remaining arguments:

# ./t.sh --test -n 5 a b c
Test: 1
Nice: 5
Remaining arguments: a b c 

# ./t.sh --test -n 5 -- ~/script --with -x --arguments a b c
Test: 1
Nice: 5
Remaining arguments: /var/www/script --with -x --arguments a b c