#11159: fix(cli): parse --profile flag after subcommand name
cli
stale
Cluster:
Gateway and Session Fixes
#### Summary
The `--profile` flag is silently ignored when placed after a subcommand name
(e.g., `openclaw gateway --profile invest`). This is because `parseCliProfileArgs`
stops intercepting flags after seeing a non-flag argument (`sawCommand`).
Multi-bot deployments using `--profile` get the wrong config — wrong API keys,
wrong state directory, wrong Telegram bot token.
lobster-biscuit
#### Repro Steps
1. Create profile config: `~/.openclaw-invest/openclaw.json`
2. Run: `openclaw gateway --profile invest --port 18795`
3. Observe: loads default `~/.openclaw/openclaw.json` instead of invest config
4. Expected: loads `~/.openclaw-invest/openclaw.json`
#### Fix
Modify `parseCliProfileArgs` to continue intercepting `--profile` (but NOT `--dev`)
even after `sawCommand = true`.
- `--dev` is intentionally left for the gateway subcommand's own Commander option
- `--profile` is now treated as a global flag regardless of position
- Existing `--dev` passthrough test unchanged and still passes
#### Changes
- `src/cli/profile.ts`: 1-line change to `sawCommand` guard condition
- `src/cli/profile.test.ts`: 4 new test cases covering post-subcommand `--profile`
#### Tests
- 22/22 vitest tests pass (18 existing + 4 new)
- `oxfmt --check` clean
- New test cases:
- `--profile` after subcommand → parsed correctly
- `--profile=NAME` after subcommand → parsed correctly
- `--dev` + `--profile` both after subcommand → `--dev` stays in argv, `--profile` intercepted
- `--dev` before subcommand + `--profile` after → rejected (mixed modes)
**Sign-Off**
- Models used: Claude Opus 4.6
- Submitter effort: Discovered during multi-bot deployment debugging (3 gateway instances). Root cause traced from wrong Telegram bot token in logs → CLI argument parsing at `profile.ts:42`.
- Agent notes: The `sawCommand` guard at profile.ts:42 was passing through ALL flags after subcommand name. `--dev` is correctly passed through (gateway has its own `--dev` Commander option at run.ts:55). Only `--profile` needs global interception for multi-instance deployments.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR adjusts CLI pre-processing so `--profile` is treated as a global flag even when it appears after the subcommand name (e.g. `openclaw gateway --profile invest`). The change is localized to `src/cli/profile.ts` by relaxing the `sawCommand` passthrough guard to still intercept `--profile`/`--profile=...` after a command token has been seen, while continuing to passthrough other flags (notably `--dev` for the `gateway` subcommand). `src/cli/profile.test.ts` adds new Vitest cases to cover post-subcommand `--profile` parsing and ensure `--dev` passthrough behavior remains intact.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is a small, well-targeted adjustment to argument parsing with focused test coverage for the newly supported CLI forms; no regressions were identified in the modified logic based on existing call sites.
- No files require special attention
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#15981: fix: include --profile in systemd service ExecStart
by MisterGuy420 · 2026-02-14
82.0%
#21459: fix(gateway): resolve port from profile config, not inherited env
by kkeeling · 2026-02-19
80.7%
#11602: fix(config): skip stale legacy config files when openclaw.json exists
by akoscz · 2026-02-08
79.5%
#14944: fix(browser): prefer openclaw profile in headless/noSandbox environ...
by BenediktSchackenberg · 2026-02-12
77.9%
#9728: fix(gateway): browser.snapshot ignores profile parameter (#9723)
by divol89 · 2026-02-05
77.7%
#10367: CLI/Ops: resilient browser fill + failover hardening + operations t...
by cluster2600 · 2026-02-06
76.9%
#11455: fix(gateway): default gateway.mode to local when unset
by AnonO6 · 2026-02-07
76.6%
#17770: refactor(cli): reuse shared option builders
by iyoda · 2026-02-16
76.5%
#11198: fix(models): strip @profile suffix from model selection
by mcaxtr · 2026-02-07
76.5%
#23301: Gateway: prevent dev reset from touching default profile state
by tristanmanchester · 2026-02-22
76.1%