diff --git a/cli.py b/cli.py index 64d0da7..456acf3 100755 --- a/cli.py +++ b/cli.py @@ -154,7 +154,7 @@ _FLAGS_WITH_VALUE = frozenset({ def _get_ordered_actions(argv: List[str]) -> List[tuple]: """ Scan argv and return list of (action_name, value) in order of appearance. - Actions: flash, pause, reset, upload, erase_all, rm, follow. + Actions: flash, pause, reset, upload, ls, erase_all, rm, follow. """ actions = [] i = 1 @@ -205,13 +205,18 @@ def _get_ordered_actions(argv: List[str]) -> List[tuple]: actions.append(('upload', [local_dir, ""])) continue if arg == '--lib': - # Upload local DIR to /lib on device - if i + 1 < len(argv): + # Upload local DIR (default: ./lib) to /lib on device + local_dir = "lib" + if i + 1 < len(argv) and not argv[i + 1].startswith('-'): local_dir = argv[i + 1] - actions.append(('upload', [local_dir, "lib"])) i += 2 else: - raise ValueError("--lib requires a directory argument") + i += 1 + actions.append(('upload', [local_dir, "lib"])) + continue + if arg == '--ls': + actions.append(('ls', None)) + i += 1 continue if arg == '-e': actions.append(('erase_all', None)) @@ -434,8 +439,16 @@ Examples: parser.add_argument( "--lib", + nargs="?", + const="lib", metavar="DIR", - help="Upload DIR recursively to /lib on device" + help="Upload DIR recursively to /lib on device. If DIR is omitted, uses local ./lib." + ) + + parser.add_argument( + "--ls", + action="store_true", + help="List files on the device root (:/)" ) parser.add_argument( @@ -527,6 +540,18 @@ Examples: print(f"Error uploading directory: {e}", file=sys.stderr) sys.exit(1) + elif action_name == 'ls': + try: + print(f"Listing files on device {port}...", file=sys.stderr) + conn = DeviceConnection(port) + items = conn.list_files('') + for name, is_dir, size in items: + marker = "d" if is_dir else "-" + print(f"{marker} {size:>8} {name}") + except Exception as e: + print(f"Error listing files: {e}", file=sys.stderr) + sys.exit(1) + elif action_name == 'erase_all': try: print(f"Erasing all code on device {port}...", file=sys.stderr)