forked from legionus/libshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-args
96 lines (85 loc) · 2.22 KB
/
shell-args
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_args-}" ]; then
__included_shell_args=1
. shell-error
### Checks that given option value is a readable file.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a readable file, outputs canonicalized file name,
### otherwise fails.
opt_check_read()
{
local value
[ -r "$2" ] &&
value="$(readlink -ev "$2")" ||
fatal "$1: $2: file not available."
printf %s "$value"
}
### Checks that given option value is an executable file.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a executable file, outputs canonicalized file name,
### otherwise fails.
opt_check_exec()
{
local value
[ -f "$2" -a -x "$2" ] &&
value="$(readlink -ev "$2")" ||
fatal "$1: $2: file not executable."
printf %s "$value"
}
### Checks that given option value is a traversable directory.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a readable file, outputs canonicalized directory name,
### otherwise fails.
opt_check_dir()
{
local value
[ -d "$2" -a -x "$2" ] &&
value="$(readlink -ev "$2")" ||
fatal "$1: $2: directory not available."
printf %s "$value"
}
### Checks that given option value is a positive decimal number.
### Arguments: $1 is option name, $2 is option value.
### If $2 is a positive decimal number, outputs it, otherwise fails.
opt_check_number()
{
[ -n "${2##0*}" -a -n "${2##*[!0-9]*}" ] &&
[ "$2" -gt 0 ] 2>/dev/null ||
fatal "$1: $2: invalid number."
printf %s "$2"
}
show_usage()
{
[ -z "$*" ] || message "$*"
echo "Try \`$PROG --help' for more information." >&2
exit 1
}
show_help()
{
fatal "show_help(): Undefined function"
}
print_version()
{
fatal "print_version() Undefined function"
}
readonly getopt_common_opts="q,v,V,h"
readonly getopt_common_longopts="quiet,verbose,version,help"
parse_common_option()
{
case "$1" in
-h|--help) show_help
;;
-q|--quiet) quiet=-q; verbose=
;;
-v|--verbose) verbose=-v; quiet=
;;
-V|--version) print_version "$PROG"
;;
*) fatal "Unrecognized option: $1"
;;
esac
}
fi #__included_shell_args