From 482c787f79c5a44584d0fbc47f2d7abbddaf00e5 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 20 Nov 2020 01:36:49 -0800 Subject: [PATCH] Add custom exception NoTerms. --- search.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/search.py b/search.py index 8ed7f58..9362f92 100644 --- a/search.py +++ b/search.py @@ -25,6 +25,9 @@ if stat.S_ISFIFO(STDIN_MODE): else: STDIN_MODE = 'terminal' +class NoTerms(Exception): + pass + class HeaderedText: def __init__(self, header, text): self.header = header @@ -125,7 +128,7 @@ def search( do_plain = not (do_glob or do_regex) if all(v == [] for v in terms.values()) and not content_args: - raise ValueError('No terms supplied') + raise NoTerms('No terms supplied') def term_matches(line, term): if not case_sensitive: @@ -232,7 +235,7 @@ def argparse_to_dict(args): 'text': text, } -def search_argparse(args): +def _search_argparse(args): generator = search(**argparse_to_dict(args)) result_count = 0 for result in generator: @@ -241,6 +244,13 @@ def search_argparse(args): if args.show_count: print('%d items.' % result_count) +def search_argparse(args): + try: + return _search_argparse(args) + except NoTerms: + print('You did not supply any search terms.') + return 1 + def main(argv): parser = argparse.ArgumentParser() @@ -280,6 +290,7 @@ def main(argv): args.content_args = parser.parse_args(content_args) else: args.content_args = None + return args.func(args) if __name__ == '__main__':