Repository navigation
Conversation
`http post -f example.com` used to fail: argparse consumes the positional arguments as a group, so the flag between METHOD and URL made `post` be parsed as the URL and `example.com` was then rejected as an invalid request item. `http example.com --form name=value` even crashed with an AssertionError in _guess_method(). Sort the raw arguments into optionals and positionals (each group keeping its original relative order) before handing them to parse_known_args(), which we still rely on for the --no-OPTION mechanism -- hence not using parse_intermixed_args(). Token classification reuses argparse's own _parse_optional() so that abbreviations, attached short values, --opt=value, negative numbers and the --no-OPTION forms are classified exactly as argparse would. Command lines containing `--` are passed through untouched: older argparse versions cannot match positionals that follow an optional and the separator.
Surri757
force-pushed
the
allow-flags-between-positionals
branch
from
November 28, 2026 06:00
3377351 to
bc755c0
Compare
1 task done
Author
|
Hi, and thanks to @meat-daemon-404 for the earlier PR #2005, which addresses the same problem of allowing options to be specified between the positional arguments METHOD and URL. Our change here modifies the same line in the same function of httpie/cli/argparser.py, so the two pull requests would necessarily conflict with each other. To avoid creating a merge conflict for the maintainers, we are withdrawing this one. This pull request was prepared with AI assistance from MiniMax Code. Thank you to the maintainers for their time. |
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.
What
Flags can now be specified anywhere among the positional arguments — the three forms below are equivalent:
Before this change,
http post -f example.comfailed with a confusing error:$ http post -f example.com usage: http [METHOD] URL [REQUEST_ITEM ...] error: argument REQUEST_ITEM: 'example.com' is not a valid valuehttp example.com --form name=value(a flag between the URL and the request items) even crashed with anAssertionErrorin_guess_method(); it now works as well.Closes #1433.
The four questions
METHODandURL(or betweenURLand the request items) breaks parsing.argparseitself ships a standard facility for intermixed parsing (parse_intermixed_args); this PR brings that capability to HTTPie's own parser.Root cause
argparsematches positional arguments as a group. Inpost -f example.com, the flag splits the positionals into two single-token runs;argparsethen assigns the first run'spostto the requiredURL(leavingMETHODunset) and the second run'sexample.comtoREQUEST_ITEM, which fails theKeyValueArgTypeconversion — hence the confusing error above.Approach
HTTPieArgumentParser.parse_args()depends onparse_known_args()to implement the--no-OPTIONmechanism (_apply_no_options()), so switching toparse_intermixed_args()is not an option (there is no known-args variant, and its behaviour is not uniform across the supported Python range). Instead, the raw argv is sorted into optionals and positionals — each group preserving its original relative order — before being handed toparse_known_args().Token classification reuses argparse's own
_parse_optional(), so abbreviations (--time 5), attached short values (-pHB),--opt=value, negative numbers (--timeout -1) and the unknown--no-OPTIONforms are classified exactly asargparsewould classify them. An option only takes the following argument as its value whenargparseitself would (i.e. it does not look like an optional), so e.g.--header -fbehaves exactly as before.Command lines containing the
--separator are passed through untouched: olderargparseversions cannot match positional arguments that follow an optional and the separator, so reordering them would break invocations that work today on those versions.Changes
httpie/cli/argparser.py: newHTTPieArgumentParser._intermixed_args()(L198) and._is_optional()(L266);parse_args()now passes the reordered arguments toparse_known_args()(L160).httpie/cli/definition.py: the "Positional arguments" group description ("These arguments come after any flags...") is no longer accurate (L52).CHANGELOG.md: unreleased entry, following the repo's existing convention (L8).tests/test_cli.py: newTestFlagsAfterPositionalArguments— 9 tests (L320).Verification
Python 3.12.14 and Python 3.11.5 (the two ends of the
argparsebehaviour range),pip install -e '.[dev]' '.[test]'.The new tests fail without the change (
python -m pytest tests/test_cli.py::TestFlagsAfterPositionalArguments, 3.12.14, against the unmodified tree):The two
--tests pass on 3.12.14 without the change because they only exercise the passthrough; the non-regression one (test_flags_before_double_dash_keep_working) does fail on 3.11.5 without the change, where the unfixed reorder turns the workinghttp -f post example.com -- name=valueintounrecognized arguments: -- name=value— which is exactly why--command lines are passed through unchanged.With the change:
python -m pytest tests/test_cli.py->47 passedon both 3.12.14 and 3.11.5python -m pytest ./httpie ./tests, Windows,NO_PROXY=localhost,127.0.0.1so the suite is not routed through a dead local proxy):bc755c0) ->3 failed, 1011 passed, 19 skipped, 4 xfailed in 183.05s5b604c3, the parent commit) ->3 failed, 1002 passed, 19 skipped, 4 xfailed in 213.21s— the failure sets are identical; the branch adds 9 passing tests and zero failuresargparseno longer quotes choices ininvalid choiceerrors)flake8with the repo'smake codestyleconfiguration -> clean (exit 0)--handling, abbreviations, attached values) run against the unmodified tree and against this branch, on both 3.12.14 and 3.11.5: 42 identical, 17 changed on each — all 17 changed cases are interleaved forms that previously failedCI failures on
bc755c0(attribution — none of them are caused by this diff)Latest check run: the test matrix run 46114581259. Every red job falls into one of three buckets:
setup-pythonphase failures (runner image infrastructure).code-style(job 140885427491, 4s) and the Ubuntu 3.7/3.8/3.9 test jobs (e.g. job 140885430841, 4s) die before any of this repository's code runs, with the log lines:GitHub's
ubuntu-latestimage (now 26.04) no longer ships the Python versions this workflow pins. The Windows runners still do — thewindows-latest, 3.7job logsInstall Python 3.7.9 in C:\\hostedtoolcache\\windows\\Pythonand runs the suite for real.Pre-existing test failures (they fail identically on the unmodified tree). The jobs that do run fail on the same cases with or without this diff:
ubuntu-latest, 3.10(job 140885430902):2 failed, 1026 passed, 5 skipped, 4 xfailed— both failures aretests/test_encoding.pybig5 charset detection.ubuntu-latest, 3.12(job 140885430613):3 failed, 1025 passed, 5 skipped, 4 xfailed— the two big5 cases plustests/test_cli_ui.py::test_naked_invocation[args3-expected_msg3], which expectsargparseto quote choices ininvalid choiceerrors; that quoting was removed in Python 3.12.5+, so this case now fails for every PR on the 3.12 leg of the matrix.windows-latest, 3.12(job 140885430991): the same three failures (3 failed, 1011 passed, 19 skipped, 4 xfailed).windows-latest, 3.7(job 140885431096): the two big5 cases only (2 failed, 1010 passed, 21 skipped, 3 xfailed, 1 xpassed).coverage(job 140885427538): the same two big5 failures on 3.10 (2 failed, 1026 passed, 5 skipped, 4 xfailed); the coverage report itself printsTOTAL ... 94%.docpasses.Still queued: the twelve
macos-13jobs.Controls — same repository, other PRs: external PR #2007 (unrelated change) and #2005 ("Fix intermixed options between METHOD and URL", the closest topic to this one) currently show the identical failure surface:
code-stylefailing in 4–5s at the samesetup-pythonstep,coveragered, the 3.7–3.9 legs dead in seconds, 3.10–3.12 red after a real run, the whole Windows matrix red, macOS queued. Upstreammasterhas no push-triggered runs retained at all (the newest runs on any branch namedmasterarepull_requestevents from 2026-08-29), so this is simply the repository's current CI state for every branch, not something introduced here.Local A/B in a single environment (this round). Windows, Python 3.12.14,
pip install -e .,NO_PROXY=localhost,127.0.0.1, full suite collected exactly as CI collects it:bc755c0):3 failed, 1011 passed, 19 skipped, 4 xfailed5b604c3, the parent commit):3 failed, 1002 passed, 19 skipped, 4 xfailedThe failure sets are identical (
test_naked_invocation[args3-expected_msg3]+ big5 x2); the branch adds 9 passing tests and zero failures. On 3.11.5,tests/test_cli_ui.py+tests/test_encoding.pyfail on exactly the two big5 cases on both trees.I have not touched the workflow or the Python version matrix; the runner image and the pinned interpreter versions are upstream infrastructure, out of scope for this PR. The local runs above use real interpreters and are reproducible with the commands given.
AI tools used: MiniMax Code (MCode). The implementation, the tests and every verification run above were produced and executed with it under a standing delegation from the account owner; no line-by-line human code review is claimed. The added tests were run locally and fail without the change.