Skip to content

Allow flags to be specified between positional arguments - #2011

Closed
Surri757 wants to merge 1 commit into
httpie:masterfrom
Surri757:allow-flags-between-positionals
Closed

Surri757 wants to merge 1 commit into
httpie:masterfrom
Surri757:allow-flags-between-positionals

Conversation

@Surri757

@Surri757 Surri757 commented Nov 28, 2026 •

Copy link
Copy Markdown

What

Flags can now be specified anywhere among the positional arguments — the three forms below are equivalent:

$ http -f post example.com      # already worked
$ http post -f example.com      # used to fail (this issue)
$ http post example.com -f      # already worked

Before this change, http post -f example.com failed 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 value

http example.com --form name=value (a flag between the URL and the request items) even crashed with an AssertionError in _guess_method(); it now works as well.

Closes #1433.

The four questions

  1. What is missing? The CLI cannot intermix options and positional arguments: a flag between METHOD and URL (or between URL and the request items) breaks parsing.
  2. Who benefits, and what changes? Every HTTPie user. Typing the command in the order you would say it ("post with a form to example.com") is a natural muscle memory, and today it fails with the URL reported as an invalid request item. Observable change: the three forms above parse identically, and the crashing form now works.
  3. Why is this not a polish? It adds a missing parser capability and changes the accepted command syntax — not a typo, wording, or formatting change.
  4. Does it fit the project's direction? HTTPie's whole point is being a human-friendly CLI, and argparse itself ships a standard facility for intermixed parsing (parse_intermixed_args); this PR brings that capability to HTTPie's own parser.

Root cause

argparse matches positional arguments as a group. In post -f example.com, the flag splits the positionals into two single-token runs; argparse then assigns the first run's post to the required URL (leaving METHOD unset) and the second run's example.com to REQUEST_ITEM, which fails the KeyValueArgType conversion — hence the confusing error above.

Approach

HTTPieArgumentParser.parse_args() depends on parse_known_args() to implement the --no-OPTION mechanism (_apply_no_options()), so switching to parse_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 to parse_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-OPTION forms are classified exactly as argparse would classify them. An option only takes the following argument as its value when argparse itself would (i.e. it does not look like an optional), so e.g. --header -f behaves exactly as before.

Command lines containing the -- separator are passed through untouched: older argparse versions 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: new HTTPieArgumentParser._intermixed_args() (L198) and ._is_optional() (L266); parse_args() now passes the reordered arguments to parse_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: new TestFlagsAfterPositionalArguments — 9 tests (L320).

Verification

Python 3.12.14 and Python 3.11.5 (the two ends of the argparse behaviour 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):

collected 9 items
... 7 FAILED, 2 passed ...
E   argparse.ArgumentError: argument REQUEST_ITEM: 'example.com' is not a valid value
7 failed, 2 passed, 2021 warnings in 0.93s

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 working http -f post example.com -- name=value into unrecognized arguments: -- name=value — which is exactly why -- command lines are passed through unchanged.

With the change:

  • python -m pytest tests/test_cli.py -> 47 passed on both 3.12.14 and 3.11.5
  • full suite, same collection as CI (python -m pytest ./httpie ./tests, Windows, NO_PROXY=localhost,127.0.0.1 so the suite is not routed through a dead local proxy):
    • 3.12.14, this branch (bc755c0) -> 3 failed, 1011 passed, 19 skipped, 4 xfailed in 183.05s
    • 3.12.14, unmodified baseline (5b604c3, 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 failures
    • the failures are pre-existing on the unmodified tree in this environment (big5 charset detection x2 on every Python version; on 3.12.5+ only: argparse no longer quotes choices in invalid choice errors)
  • flake8 with the repo's make codestyle configuration -> clean (exit 0)
  • a 59-case differential battery (existing forms, error paths, -- 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 failed

CI 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:

  1. setup-python phase 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:

    ##[error]The version '3.9' with architecture 'x64' was not found for Ubuntu 26.04.
    ##[error]The version '3.7' with architecture 'x64' was not found for Ubuntu 26.04.
    

    GitHub's ubuntu-latest image (now 26.04) no longer ships the Python versions this workflow pins. The Windows runners still do — the windows-latest, 3.7 job logs Install Python 3.7.9 in C:\\hostedtoolcache\\windows\\Python and runs the suite for real.

  2. 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 are tests/test_encoding.py big5 charset detection.
    • ubuntu-latest, 3.12 (job 140885430613): 3 failed, 1025 passed, 5 skipped, 4 xfailed — the two big5 cases plus tests/test_cli_ui.py::test_naked_invocation[args3-expected_msg3], which expects argparse to quote choices in invalid choice errors; 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 prints TOTAL ... 94%.
    • doc passes.
  3. Still queued: the twelve macos-13 jobs.

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-style failing in 4–5s at the same setup-python step, coverage red, 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. Upstream master has no push-triggered runs retained at all (the newest runs on any branch named master are pull_request events 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:

  • this branch (bc755c0): 3 failed, 1011 passed, 19 skipped, 4 xfailed
  • unmodified baseline (5b604c3, the parent commit): 3 failed, 1002 passed, 19 skipped, 4 xfailed

The 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.py fail 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.

`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
Surri757 force-pushed the allow-flags-between-positionals branch from 3377351 to bc755c0 Compare November 28, 2026 06:00
@Surri757

Copy link
Copy Markdown
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.

@Surri757 Surri757 closed this Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow flags to be specified after method

1 participant