#!/bin/bash
#  args - Bash argument handling library

set -e


######
# Argument Checking

check_args() {
	local tst=$1
	local expect=$2
	shift 2
	local nargs="${#@}"
	assert_usage "wrong number of arguments ($nargs $tst $expect)" \
		[ ! $nargs $tst $expect ]
}

# has_args() - Asserts there are exactly `n` ($1) arguments ($@).
has_args() { check_args -ne "$@"; }

# min_args() - Asserts there are at least `n` ($1) arguments ($@).
min_args() { check_args -lt "$@"; }

# max_args() - Asserts there are at most `n` ($1) arguments ($@).
max_args() { check_args -gt "$@"; }

# min_max_args() - Asserts there are at least `n` ($1) and at most `m`
# ($2) arguments ($@).
min_max_args() {
	local n=$1
	local m=$2
	shift 2
	assert_usage "wrong number of arguments ($# < $n or $# > $m)" \
		[ $# -ge $n -a $# -le $m ]
}


######
# Argument Quoting

# arg_quote() - Quotes a single argument ($1).
arg_quote() {
	has_args 1 "$@"
	# handle existing quotes
	local value=${1//\'/\'\\\'\'}
	printf "'%s'" "$value"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Sat Jul 8 19:32:31 PDT 2017 by mcsh d14 v0.22.0.