Repository navigation
fix: Explain how with-connect works in --help and reject invalid --port - #63
Conversation
… someone types a subcommand like start.
| parser.error(f"unrecognized arguments: {' '.join(unknown)}") | ||
| if unknown: | ||
| parser.error( | ||
| f"unexpected argument '{unknown[0]}', with-connect has no subcommands. " |
There was a problem hiding this comment.
The new 'no subcommands' error fires for any stray positional value, including word-split values from this repo's own action.yml, so users get a misleading diagnosis.
action.yml builds ARGS="$ARGS -e $line" and expands with-connect $ARGS without quotes. An env input like FOO=bar baz (or a config-file/image path containing a space) becomes -e FOO=bar baz. argparse leaves baz unknown, and the user sees "unexpected argument 'baz', with-connect has no subcommands". The real cause is a quoting/value problem. Before this change the plain "unrecognized arguments: baz" was at least neutral. The same happens with with-connect --license a.lic b.lic. Consider limiting the subcommand hint to known verb-like words (start/run/stop/up/down), or wording it as "unexpected positional argument" without claiming the user tried a subcommand.
There was a problem hiding this comment.
Removed the custom error. Unknown arguments now get argparse's standard message, so nothing guesses at a subcommand.
| assert "CONTAINER_ID" in result.stdout | ||
|
|
||
|
|
||
| def run_cli(*args): |
There was a problem hiding this comment.
The new run_cli helper isn't reused by the existing subprocess-based tests, and the pure argument-parsing tests spawn a full interpreter when the in-process parse_with_argv helper would do.
test_custom_port, test_stop_argument_in_help, test_stop_nonexistent_container and others still inline the same subprocess.run([sys.executable, 'main.py', ...], capture_output=True, text=True). The four new error-message tests each start a new Python process that imports docker and rsconnect just to exercise argparse. Using parse_with_argv with pytest.raises(SystemExit) and capsys (as the last two new tests already do for the happy path) avoids the process startup cost, and migrating the old call sites to run_cli removes the duplication.
There was a problem hiding this comment.
The four subprocess error tests went away with the custom error, along with run_cli. The new port and help tests run in-process. pytest isn't a dependency here and CI runs test_main.py directly, so they catch SystemExit and capture output with contextlib instead of pytest.raises and capsys. Moving the older subprocess tests to a shared helper would be a separate cleanup.
tdstein
left a comment
There was a problem hiding this comment.
Posted some Claude generated review comments. Overall, I'm wondering if we should be using parser.print_help(); parser.exit() from argparse instead of using a custom error message.
…port right away. Add how to stop the container to --help.
I sampled what other popular CLIs do with a bad argument. Most print a short error instead of the whole help so we followed that simpler implementation here. Happy to discuss further. |
| def port_number(value: str) -> int: | ||
| """Parse --port. Docker can only map a fixed host port from 1 to 65535.""" | ||
| try: | ||
| port = int(value) | ||
| except ValueError: | ||
| raise argparse.ArgumentTypeError(f"invalid port value: '{value}'") | ||
| if not 1 <= port <= 65535: | ||
| raise argparse.ArgumentTypeError(f"{port} is not a valid port. Use a free port from 1 to 65535.") | ||
| return port |
|
Agents running
with-connect --helponly saw a list of flags. They guessed astartsubcommand and flags that don't exist, like--json,--nameand--data-dir, and their runs failed. See posit-dev/connect#44594.The help now says what with-connect prints, that the container keeps running, how to stop it, and what
-- COMMANDdoes. The--stophelp also saysCONTAINER_IDmust be exported when no ID is given. Unknown arguments likestartor--jsonstill get argparse's standard error.--portnow rejects values outside 1 to 65535 right away. Before,--port 0was accepted and then failed with a misleading timeout error.The README eval example did not export the variables, so
with-connect --stopcould not seeCONTAINER_IDin a clean shell. It now exports them.Fixes #62. Follow-up work is tracked in #61, #64 and #65.