Add --dirs, --files to restrict search.

This commit is contained in:
voussoir 2020-12-05 14:49:10 -08:00
parent c9b05f60a4
commit e9b5af6e4f

View file

@ -113,6 +113,8 @@ def search(
do_strip=False, do_strip=False,
line_numbers=False, line_numbers=False,
local_only=False, local_only=False,
only_dirs=False,
only_files=False,
root_path='.', root_path='.',
text=None, text=None,
): ):
@ -176,6 +178,10 @@ def search(
# if index % 10 == 0: # if index % 10 == 0:
# print(index, end='\r', flush=True) # print(index, end='\r', flush=True)
if isinstance(search_object, pathclass.Path): if isinstance(search_object, pathclass.Path):
if only_files and not search_object.is_file:
continue
if only_dirs and not search_object.is_dir:
continue
search_text = search_object.basename search_text = search_object.basename
result_text = search_object.absolute_path result_text = search_object.absolute_path
elif isinstance(search_object, HeaderedText): elif isinstance(search_object, HeaderedText):
@ -232,6 +238,8 @@ def argparse_to_dict(args):
'do_strip': args.do_strip, 'do_strip': args.do_strip,
'local_only': args.local_only, 'local_only': args.local_only,
'line_numbers': args.line_numbers, 'line_numbers': args.line_numbers,
'only_dirs': args.only_dirs,
'only_files': args.only_files,
'text': text, 'text': text,
} }
@ -286,6 +294,8 @@ def main(argv):
parser.add_argument('--local', dest='local_only', action='store_true') parser.add_argument('--local', dest='local_only', action='store_true')
parser.add_argument('--regex', dest='do_regex', action='store_true') parser.add_argument('--regex', dest='do_regex', action='store_true')
parser.add_argument('--text', dest='text', default=None) parser.add_argument('--text', dest='text', default=None)
parser.add_argument('--dirs', '--folders', dest='only_dirs', action='store_true')
parser.add_argument('--files', dest='only_files', action='store_true')
parser.set_defaults(func=search_argparse) parser.set_defaults(func=search_argparse)
args = parser.parse_args(name_args) args = parser.parse_args(name_args)