Add custom exception NoTerms.

This commit is contained in:
voussoir 2020-11-20 01:36:49 -08:00
parent 46545edaa5
commit 482c787f79

View file

@ -25,6 +25,9 @@ if stat.S_ISFIFO(STDIN_MODE):
else: else:
STDIN_MODE = 'terminal' STDIN_MODE = 'terminal'
class NoTerms(Exception):
pass
class HeaderedText: class HeaderedText:
def __init__(self, header, text): def __init__(self, header, text):
self.header = header self.header = header
@ -125,7 +128,7 @@ def search(
do_plain = not (do_glob or do_regex) do_plain = not (do_glob or do_regex)
if all(v == [] for v in terms.values()) and not content_args: 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): def term_matches(line, term):
if not case_sensitive: if not case_sensitive:
@ -232,7 +235,7 @@ def argparse_to_dict(args):
'text': text, 'text': text,
} }
def search_argparse(args): def _search_argparse(args):
generator = search(**argparse_to_dict(args)) generator = search(**argparse_to_dict(args))
result_count = 0 result_count = 0
for result in generator: for result in generator:
@ -241,6 +244,13 @@ def search_argparse(args):
if args.show_count: if args.show_count:
print('%d items.' % result_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): def main(argv):
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@ -280,6 +290,7 @@ def main(argv):
args.content_args = parser.parse_args(content_args) args.content_args = parser.parse_args(content_args)
else: else:
args.content_args = None args.content_args = None
return args.func(args) return args.func(args)
if __name__ == '__main__': if __name__ == '__main__':