Skip to content

Commit

Permalink
improve usage function to simplify exit code
Browse files Browse the repository at this point in the history
  • Loading branch information
oldratlee committed May 24, 2018
1 parent e65f32d commit 5a50de8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ readonly PROG="`basename "$0"`"
usage() {
[ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout

[ $# -gt 1 ] && { echo "$2"; echo; } > $out

> $out cat <<EOF
Usage: ${PROG} [OPTION]... [command [command_args ...]]
Run command and put output to system clipper.
Expand Down Expand Up @@ -62,9 +64,7 @@ while [ $# -gt 0 ]; do
break
;;
-*)
echo "${PROG}: unrecognized option '$1'" 1>&2
echo 1>&2
usage 2;
usage 2 "${PROG}: unrecognized option '$1'"
;;
*)
# if not option, treat all follow args as command
Expand Down
2 changes: 1 addition & 1 deletion docs/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Example:
find-in-jars 'log4j\.properties'
find-in-jars '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
find-in-jars 'log4j\.properties$' -d /path/to/find/directory
find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d path/to/find/dir2
find-in-jars 'log4j\.properties' -e jar -e zip
find-in-jars 'log4j\.properties' -s ' <-> '
Expand Down
12 changes: 6 additions & 6 deletions find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ readonly PROG="`basename "$0"`"
usage() {
[ -n "$1" -a "$1" != 0 ] && local out=/dev/stderr || local out=/dev/stdout

[ $# -gt 1 ] && { echo "$2"; echo; } > $out

> $out cat <<EOF
Usage: ${PROG} [OPTION]... PATTERN
Find file in the jar files under specified directory(recursive, include subdirectory).
Expand All @@ -25,7 +27,7 @@ Example:
${PROG} 'log4j\.properties'
${PROG} '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
${PROG} 'log4j\.properties$' -d /path/to/find/directory
${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d path/to/find/dir2
${PROG} 'log4j\.properties' -e jar -e zip
${PROG} 'log4j\.properties' -s ' <-> '
Expand Down Expand Up @@ -106,9 +108,7 @@ while [ $# -gt 0 ]; do
break
;;
-*)
echo "${PROG}: unrecognized option '$1'" 1>&2
echo 1>&2
usage 2;
usage 2 "${PROG}: unrecognized option '$1'"
;;
*)
args=("${args[@]}" "$1")
Expand All @@ -120,8 +120,8 @@ dirs=${dirs:-.}
extension=${extension:-jar}
seperator="${seperator:-!}"

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

# convert dirs to Absolute Path.
Expand Down

0 comments on commit 5a50de8

Please sign in to comment.