diff --git a/ui/fixtures/cmd.basic.json b/ui/fixtures/cmd.basic.json index f7dbd64..780fc74 100644 --- a/ui/fixtures/cmd.basic.json +++ b/ui/fixtures/cmd.basic.json @@ -47,6 +47,12 @@ "exit": 1, "stderr": "ERROR: file not found: 'newfile.txt'\n" }, +{ + "title": "Test file with no tag, with --empty --json", + "cmd": [ "list", "--json", "--empty", "newfile.txt" ], + "exit": 0, + "stdout": "{}\n" +}, { "title": "invalid tag", "cmd": [ "modify", "+tag", "newfile.txt" ], diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index 0e85042..fd689e0 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -1305,16 +1305,18 @@ def cmd_interactive(options, persona, objstr, cache): inp = sys.stdin.readline().strip() - if inp == '1': + if not inp: + continue + elif inp == '1': idx = max(0, idx - 1) continue - if inp == '2': + elif inp == '2': idx = min(len(files) - 1, idx + 1) continue - if inp == '3': + elif inp == '3': files, idx = getnextfile(files, idx) continue - if inp == '4': + elif inp == '4': files = sorted(curfile.parent.iterdir()) try: idx = files.index(curfile) @@ -1322,7 +1324,7 @@ def cmd_interactive(options, persona, objstr, cache): print('WARNING: File no longer present.') idx = 0 continue - if inp == '5': + elif inp == '5': files = [ pathlib.Path(x) for x in options.files ] try: idx = files.index(curfile) @@ -1330,7 +1332,7 @@ def cmd_interactive(options, persona, objstr, cache): print('WARNING: File not present.') idx = 0 continue - if inp == '6': + elif inp == '6': print('Tag?') try: tag, value = sys.stdin.readline().strip().split('=', 1) @@ -1345,11 +1347,11 @@ def cmd_interactive(options, persona, objstr, cache): objstr.loadobj(metadata) continue - if inp == '7': + elif inp == '7': subprocess.run(('open', curfile)) continue - if inp.lower() == 'q': + elif inp.lower() == 'q': break try: @@ -1421,6 +1423,10 @@ def cmd_list(options, persona, objstr, cache): objs = objstr.by_file(i) except (FileNotFoundError, KeyError) as e: + if options.empty and options.json: + print('{}') + continue + print('ERROR: file not found: %s' % repr(i), file=sys.stderr) exit = 1 continue @@ -1815,6 +1821,8 @@ def main(): parser_list = subparsers.add_parser('list', help='list tags on file(s)') parser_list.add_argument('--json', action='store_true', help='output data as series of JSON objects') + parser_list.add_argument('--empty', action='store_true', + help='output an empty JSON object if file not found or not metadata') parser_list.add_argument('files', nargs='+', help='files to modify') parser_list.set_defaults(func=cmd_list)