From a311e9e3d419cf447c8a4cf2beed4a341e8387e3 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 18 Aug 2022 12:32:10 +1000 Subject: [PATCH] tools/mpremote: Allow + terminator for fs commands. Signed-off-by: Jim Mussared --- docs/reference/mpremote.rst | 5 +++++ tools/mpremote/mpremote/main.py | 36 ++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/reference/mpremote.rst b/docs/reference/mpremote.rst index 2bbed56b89..80290657f4 100644 --- a/docs/reference/mpremote.rst +++ b/docs/reference/mpremote.rst @@ -30,6 +30,9 @@ For REPL access, running ``mpremote`` without any arguments is usually all that is needed. ``mpremote`` also supports a set of commands given at the command line which will perform various actions on remote MicroPython devices. +For commands that support multiple arguments (e.g. a list of files), the +argument list can be terminated with ``+``. + The full list of supported commands are: - connect to a specified device via a device-name shortcut: @@ -248,3 +251,5 @@ Examples mpremote cp main.py : mpremote cp -r dir/ : + + mpremote cp a.py b.py : + repl diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index e0266fd719..73cd5e153b 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -300,6 +300,19 @@ def show_progress_bar(size, total_size): ) +# Get all args up to the terminator ("+"). +# The passed args will be updated with these ones removed. +def get_fs_args(args): + n = 0 + for src in args: + if src == "+": + break + n += 1 + fs_args = args[:n] + args[:] = args[n + 1 :] + return fs_args + + def do_filesystem(pyb, args): def _list_recursive(files, path): if os.path.isdir(path): @@ -308,16 +321,18 @@ def do_filesystem(pyb, args): else: files.append(os.path.split(path)) - # Don't be verbose when using cat, so output can be redirected to something. - verbose = args[0] != "cat" + fs_args = get_fs_args(args) - if args[0] == "cp" and args[1] == "-r": - args.pop(0) - args.pop(0) - assert args[-1] == ":" - args.pop() + # Don't be verbose when using cat, so output can be redirected to something. + verbose = fs_args[0] != "cat" + + if fs_args[0] == "cp" and fs_args[1] == "-r": + fs_args.pop(0) + fs_args.pop(0) + assert fs_args[-1] == ":" + fs_args.pop() src_files = [] - for path in args: + for path in fs_args: _list_recursive(src_files, path) known_dirs = {""} pyb.exec_("import uos") @@ -335,8 +350,9 @@ def do_filesystem(pyb, args): verbose=verbose, ) else: - pyboard.filesystem_command(pyb, args, progress_callback=show_progress_bar, verbose=verbose) - args.clear() + pyboard.filesystem_command( + pyb, fs_args, progress_callback=show_progress_bar, verbose=verbose + ) def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject):