Pass custom arguments to the app script from flet run#6721
Merged
Conversation
Everything after a `--` separator on a `flet run` command line is now forwarded to the app script as `sys.argv[1:]` instead of being parsed by Flet, e.g. `flet run --web main.py -- --dataset big.csv --verbose`. The child process was previously launched as `python -u <script>` with no extra arguments, so there was no way to parameterize an app under hot reload - a flag meant for the app hit the CLI's parser and was rejected as an unrecognized argument. The separator is stripped in `main()` before the parser (and `set_default_subparser`) sees the command line, so app arguments can never be mistaken for Flet's own options or for a subcommand name. Arguments that don't look like options are also accepted without the separator, and unrecognized options now hint at `--` when the command is `run`. Forwarded arguments are re-applied on every hot reload and work in every run mode, including `-m` module invocations. Also makes `get_parser()` free of `sys.argv` side effects: `set_default_subparser` now takes and returns an argv list, which the crocodocs CLI doc generator was previously mutating by importing it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
flet runhad no way to pass arguments to the app script - the child process was always launched aspython -u <script>, so a flag meant for the app was consumed by the CLI's own parser:Everything after a bare
--separator is now forwarded to the script and arrives there assys.argv[1:]:Behavior
main()before the parser (andset_default_subparser) sees the command line, so app arguments can never be read as Flet's own options, andflet main.py -- buildno longer suppresses the defaultrunsubcommand.flet run -w main.py -- --webruns Flet's web mode and passes--webto the app.flet run main.py big.csv.--with any other command errors explicitly instead of silently dropping the arguments.--web,--ios,--android) and with-mmodule invocations.Incidental fix
get_parser()no longer mutatessys.argv.set_default_subparsernow takes and returns an argv list instead of inserting into the global - the crocodocs CLI doc generator importsget_parser()and was having its ownsys.argvrewritten as a side effect.Tests
New
packages/flet-cli/tests/test_run_script_args.pycovers separator handling, option/argument precedence, the implicitrunsubcommand, subcommand-named script arguments, and both error paths. Verified end-to-end that the child process is launched aspython3 -u app.py --manual -x 1.Docs: new Passing arguments to your app section; the
flet runCLI reference page is generated from the parser and picks up the new positional automatically.Summary by Sourcery
Allow passing custom command-line arguments to app scripts invoked via
flet run, while tightening CLI parsing behavior around default subcommands and separators.New Features:
--separator fromflet runto the app script, exposed assys.argv[1:]and honored across all run modes and module invocations.Enhancements:
sys.argv, makerunthe default subcommand via an argv-based helper, and provide clearer errors and hints when options are unrecognized or separators are misused.Documentation:
--separator, script argument handling, and behavior across run modes.flet runargument forwarding capability and its behavior.Tests:
flet runscript argument handling, including separator semantics, interaction with Flet options, implicitrunsubcommand behavior, subcommand-like script arguments, and error cases for invalid options and separator use.