optparse
optparse copied to clipboard
Add handling of "--"
- Add handling of "--" to split script arguments from not to parse arguments
- Add -h as also possible help request
- Mark args with value=... as flags
- Add optional ARGUMENTS variable for additional arguments for usage()
- Move usage for -? in usage() below other descriptions
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