Skip to content

Commit 5a50de8

Browse files
committed
improve usage function to simplify exit code
1 parent e65f32d commit 5a50de8

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

c

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ readonly PROG="`basename "$0"`"
1717
usage() {
1818
[ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout
1919

20+
[ $# -gt 1 ] && { echo "$2"; echo; } > $out
21+
2022
> $out cat <<EOF
2123
Usage: ${PROG} [OPTION]... [command [command_args ...]]
2224
Run command and put output to system clipper.
@@ -62,9 +64,7 @@ while [ $# -gt 0 ]; do
6264
break
6365
;;
6466
-*)
65-
echo "${PROG}: unrecognized option '$1'" 1>&2
66-
echo 1>&2
67-
usage 2;
67+
usage 2 "${PROG}: unrecognized option '$1'"
6868
;;
6969
*)
7070
# if not option, treat all follow args as command

docs/java.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ Example:
371371
find-in-jars 'log4j\.properties'
372372
find-in-jars '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
373373
find-in-jars 'log4j\.properties$' -d /path/to/find/directory
374-
find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
374+
find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d path/to/find/dir2
375375
find-in-jars 'log4j\.properties' -e jar -e zip
376376
find-in-jars 'log4j\.properties' -s ' <-> '
377377

find-in-jars

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ readonly PROG="`basename "$0"`"
1616
usage() {
1717
[ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout
1818

19+
[ $# -gt 1 ] && { echo "$2"; echo; } > $out
20+
1921
> $out cat <<EOF
2022
Usage: ${PROG} [OPTION]... PATTERN
2123
Find file in the jar files under specified directory(recursive, include subdirectory).
@@ -25,7 +27,7 @@ Example:
2527
${PROG} 'log4j\.properties'
2628
${PROG} '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
2729
${PROG} 'log4j\.properties$' -d /path/to/find/directory
28-
${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
30+
${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d path/to/find/dir2
2931
${PROG} 'log4j\.properties' -e jar -e zip
3032
${PROG} 'log4j\.properties' -s ' <-> '
3133
@@ -106,9 +108,7 @@ while [ $# -gt 0 ]; do
106108
break
107109
;;
108110
-*)
109-
echo "${PROG}: unrecognized option '$1'" 1>&2
110-
echo 1>&2
111-
usage 2;
111+
usage 2 "${PROG}: unrecognized option '$1'"
112112
;;
113113
*)
114114
args=("${args[@]}" "$1")
@@ -120,8 +120,8 @@ dirs=${dirs:-.}
120120
extension=${extension:-jar}
121121
seperator="${seperator:-!}"
122122

123-
[ "${#args[@]}" -eq 0 ] && { echo "No find file pattern!" 1>&2 ; usage 1; }
124-
[ "${#args[@]}" -gt 1 ] && { echo "More than 1 file pattern!" 1>&2 ; usage 1; }
123+
[ "${#args[@]}" -eq 0 ] && usage 1 "No find file pattern!"
124+
[ "${#args[@]}" -gt 1 ] && usage 1 "More than 1 file pattern!"
125125
readonly pattern="${args[0]}"
126126

127127
# convert dirs to Absolute Path.

0 commit comments

Comments
 (0)