diff --git a/src/calibre/db/cli/cmd_search.py b/src/calibre/db/cli/cmd_search.py index 2f6f22ae56..f07087352e 100644 --- a/src/calibre/db/cli/cmd_search.py +++ b/src/calibre/db/cli/cmd_search.py @@ -4,19 +4,50 @@ from __future__ import absolute_import, division, print_function, unicode_literals -readonly = False +from calibre import prints + +readonly = True version = 0 # change this if you change signature of implementation() -def implementation(db, notify_changes, *args): - is_remote = notify_changes is not None - is_remote +def implementation(db, notify_changes, query): + return db.search(query) def option_parser(get_parser, args): - pass + parser = get_parser( + _( + '''\ +%prog search [options] search expression + +Search the library for the specified search term, returning a comma separated +list of book ids matching the search expression. The output format is useful +to feed into other commands that accept a list of ids as input. + +The search expression can be anything from calibre's powerful search query +language, for example: {0} +''' + ).format('author:asimov title:robot') + ) + parser.add_option( + '-l', + '--limit', + default=-1, + type=int, + help=_('The maximum number of results to return. Default is all results.') + ) + return parser def main(opts, args, dbctx): - raise NotImplementedError('TODO: implement this') + if len(args) < 1: + raise SystemExit(_('Error: You must specify the search expression')) + q = ' '.join(args) + ids = dbctx.run('search', q) + if not ids: + raise SystemExit(_('No books matching the search expression:') + ' ' + q) + ids = sorted(ids) + if opts.limit > -1: + ids = ids[:opts.limit] + prints(','.join(map(str, ids)), end='') return 0 diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index 40b368cdc4..6d2af6b403 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -23,7 +23,7 @@ COMMANDS = ( 'set_metadata', 'export', 'catalog', 'saved_searches', 'add_custom_column', 'custom_columns', 'remove_custom_column', 'set_custom', 'restore_database', 'check_library', 'list_categories', 'backup_metadata', 'clone', 'embed_metadata', - # 'search' + 'search' )