Skip to content

Commit e65f32d

Browse files
committed
add extension option for find-in-jars
1 parent 24c8378 commit e65f32d

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

docs/java.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -340,17 +340,27 @@ class paths to find:
340340
find-in-jars 'log4j\.properties'
341341
find-in-jars 'log4j\.xml$'
342342
find-in-jars log4j\\.xml$ # 和上面命令一样,Shell转义的不同写法而已
343-
find-in-jars 'log4j(\.properties|\.xml)$'
343+
find-in-jars 'log4j\.(properties|xml)$'
344344
345345
# -d选项 指定 查找目录(覆盖缺省的当前目录)
346346
find-in-jars 'log4j\.properties$' -d /path/to/find/directory
347-
# 支持多个查找目录
347+
# 支持多个查找目录,多次指定这个选项即可
348348
find-in-jars 'log4j\.properties' -d /path/to/find/directory1 -d /path/to/find/directory2
349349
350+
# -e选项 指定 查找`zip`文件的扩展名,缺省是`jar`
351+
find-in-jars 'log4j\.properties' -e zip
352+
# 支持多种查找扩展名,多次指定这个选项即可
353+
find-in-jars 'log4j\.properties' -e jar -e zip
354+
350355
# -a选项 指定 查找结果中的Jar文件使用绝对路径
351356
# 分享给别人时,Jar文件路径是完整的,方便别人找到文件
352357
find-in-jars 'log4j\.properties' -a
353358
359+
# -s选项 指定 查找结果中的Jar文件和Jar文件里的查找Entry间分隔符,缺省是『!』
360+
# 方便你喜欢的人眼查看,或是与工具脚本如`awk`的处理
361+
find-in-jars 'log4j\.properties' -s ' <-> '
362+
find-in-jars 'log4j\.properties' -s ' ' | awk '{print $2}'
363+
354364
# 帮助信息
355365
$ find-in-jars -h
356366
Usage: find-in-jars [OPTION]... PATTERN
@@ -359,20 +369,28 @@ The pattern default is *extended* regex.
359369
360370
Example:
361371
find-in-jars 'log4j\.properties'
362-
find-in-jars '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root
372+
find-in-jars '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
363373
find-in-jars 'log4j\.properties$' -d /path/to/find/directory
364374
find-in-jars 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
375+
find-in-jars 'log4j\.properties' -e jar -e zip
376+
find-in-jars 'log4j\.properties' -s ' <-> '
365377
366-
Options:
378+
Find control:
367379
-d, --dir the directory that find jar files, default is current directory.
368380
this option can specify multiply times to find in multiply directories.
369-
-s, --seperator seperator for jar file and file entry, default is `!'.
381+
-e, --extension set find file extension, default is jar.
382+
this option can specify multiply times to find in multiply extension.
370383
-E, --extended-regexp PATTERN is an extended regular expression (*default*)
371384
-F, --fixed-strings PATTERN is a set of newline-separated strings
372385
-G, --basic-regexp PATTERN is a basic regular expression
373386
-P, --perl-regexp PATTERN is a Perl regular expression
374387
-i, --ignore-case ignore case distinctions
388+
389+
Output control:
375390
-a, --absolute-path always print absolute path of jar file
391+
-s, --seperator seperator for jar file and file entry, default is `!'.
392+
393+
Miscellaneous:
376394
-h, --help display this help and exit
377395
```
378396

find-in-jars

+25-4
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,28 @@ The pattern default is *extended* regex.
2323
2424
Example:
2525
${PROG} 'log4j\.properties'
26-
${PROG} '^log4j(\.properties|\.xml)$' # search file log4j.properties/log4j.xml at zip root
26+
${PROG} '^log4j\.(properties|xml)$' # search file log4j.properties/log4j.xml at zip root
2727
${PROG} 'log4j\.properties$' -d /path/to/find/directory
2828
${PROG} 'log4j\.properties' -d /path/to/find/dir1 -d /path/to/find/dir2
29+
${PROG} 'log4j\.properties' -e jar -e zip
30+
${PROG} 'log4j\.properties' -s ' <-> '
2931
30-
Options:
32+
Find control:
3133
-d, --dir the directory that find jar files, default is current directory.
3234
this option can specify multiply times to find in multiply directories.
33-
-s, --seperator seperator for jar file and file entry, default is \`!'.
35+
-e, --extension set find file extension, default is jar.
36+
this option can specify multiply times to find in multiply extension.
3437
-E, --extended-regexp PATTERN is an extended regular expression (*default*)
3538
-F, --fixed-strings PATTERN is a set of newline-separated strings
3639
-G, --basic-regexp PATTERN is a basic regular expression
3740
-P, --perl-regexp PATTERN is a Perl regular expression
3841
-i, --ignore-case ignore case distinctions
42+
43+
Output control:
3944
-a, --absolute-path always print absolute path of jar file
45+
-s, --seperator seperator for jar file and file entry, default is \`!'.
46+
47+
Miscellaneous:
4048
-h, --help display this help and exit
4149
EOF
4250

@@ -57,6 +65,10 @@ while [ $# -gt 0 ]; do
5765
dirs=("${dirs[@]}" "$2")
5866
shift 2
5967
;;
68+
-e|--extension)
69+
extension=("${extension[@]}" "$2")
70+
shift 2
71+
;;
6072
-s|--seperator)
6173
seperator="$2"
6274
shift 2
@@ -105,6 +117,7 @@ while [ $# -gt 0 ]; do
105117
esac
106118
done
107119
dirs=${dirs:-.}
120+
extension=${extension:-jar}
108121
seperator="${seperator:-!}"
109122

110123
[ "${#args[@]}" -eq 0 ] && { echo "No find file pattern!" 1>&2 ; usage 1; }
@@ -122,6 +135,14 @@ $use_absolute_path && {
122135
dirs=( "${tmp_dirs[@]}" )
123136
}
124137

138+
# convert to find -iname options
139+
for e in "${extension[@]}"; do
140+
[ 0 -eq "${#find_iname_options[@]}" ] &&
141+
find_iname_options=( -iname "*.$e" ) ||
142+
find_iname_options=( "${find_iname_options[@]}" -o -iname "*.$e" )
143+
done
144+
145+
125146
################################################################################
126147
# Check the existence of command for listing zip entry!
127148
################################################################################
@@ -194,7 +215,7 @@ clearResponsiveMessage() {
194215
# find logic
195216
################################################################################
196217

197-
readonly jar_files="$(find "${dirs[@]}" -iname '*.jar' -type f)"
218+
readonly jar_files="$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)"
198219
readonly total_count="$(echo "$jar_files" | wc -l)"
199220
[ -z "$jar_files" ] && {
200221
echo "No jar found!" 1>&2

0 commit comments

Comments
 (0)