Allow options to be specified after non-option arguments in ParseParameters, matching GNU option parsing conventions (getopt_long style).
Currently in master, once the first non-dash argument was encountered, all subsequent arguments were collected verbatim into m_command. This meant -rpcwallet and similar options were silently ignored when placed after a command or its positional args. The same misspelled option before the command would be a clear error; after the command, it was silently treated as a positional argument — potentially triggering unintended RPC behaviour.
With this change:
- Valid options after a command are stored in
command_line_optionsexactly as if they appeared before the command. - Unrecognized options after a command are always errors (no silent fallback to positional).
- A
--separator stops option processing; all subsequent arguments are treated as positional regardless of leading dashes. bitcoin-cli'sCommandLineRPCis updated to derive its args vector fromgArgs.GetCommand()rather than re-scanning rawargv.bitcoin-walletbenefits automatically: options such as-walletplaced after a command are now parsed correctly instead of being rejected as unexpected positional arguments.
<details> <summary><i><ins>Before and after:</ins></i> options after a command are now recognized...</summary>
<br>
-rpcwallet after the command and its positional args:
Before (
master) —-rpcwalletis silently ignored; request goes to the wrong wallet:$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions -rpcwallet=nonExistent [ ]After — correctly routes to the named wallet and fails with the expected error:
$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions -rpcwallet=nonExistent error code: -18 error message: Requested wallet does not exist or is not loaded
getaddressinfo with -rpcwallet appended:
Before (
master) —-rpcwalletbecomes an extra positional arg;getaddressinfoerrors with its usage string:$ bitcoin-cli -regtest -datadir=/tmp/btc getaddressinfo bcrt1q... -rpcwallet=nonExistent error message: getaddressinfo "address" ...After — wallet is correctly selected:
$ bitcoin-cli -regtest -datadir=/tmp/btc getaddressinfo bcrt1q... -rpcwallet=nonExistent error code: -18 error message: Requested wallet does not exist or is not loaded
Multi-wallet: -rpcwallet no longer needs to come before the command:
Before (
master):$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions error code: -19 error message: ...specify the "-rpcwallet=<walletname>" option before the command...$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions -rpcwallet=mywallet error code: -19 ← still fails; option after command was ignored error message: ...specify the "-rpcwallet=<walletname>" option before the command...After:
— error message updated:
$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions error code: -19 error message: ...specify the "-rpcwallet=<walletname>" option before or after the command...— command executed successfully:
$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions -rpcwallet=mywallet [ { "address": "bcrt1q...", "category": "immature", "amount": 50.00000000, ... } ]
</details>
<details> <summary><i><ins>Before and after:</ins></i> misspelled options and the <code>--</code> separator...</summary>
<br>
Misspelled option is now an error rather than a silent positional arg:
Before (master) — -rpcwalllet (note triple-l) silently absorbed as the label positional arg:
```
$ bitcoin-cli -regtest -datadir=/tmp/btc listtransactions <hash> -rpcwalllet=foo
{ ... } ← result returned, -rpcwallet went unnoticed
```
After:
```
$ bitcoin-cli -regtest -datadir=/tmp/btc getblock <hash> -rpcwalllet=foo
error: Invalid parameter -rpcwalllet
```
-- separator: pass a dash-prefixed string as a positional RPC argument:
$ bitcoin-cli -regtest -datadir=/tmp/btc somecommand -- -not-an-option
Everything after `--` is treated as a positional argument, so `-not-an-option` is passed to the RPC unchanged.
</details>
<details> <summary><i><ins>Before and after:</ins></i> <code>bitcoin-wallet</code> commands...</summary>
<br>
-wallet option placed after the command:
Before (master) — option treated as an unexpected positional arg:
$ bitcoin-wallet -regtest -datadir=/tmp/btc create -wallet=myWallet
Error: Additional arguments provided (-wallet=myWallet). Methods do not take arguments. Please refer to -help.
After:
$ bitcoin-wallet -regtest -datadir=/tmp/btc create -wallet=myWallet
Topping up keypool...
Wallet info
Name: myWallet
Format: sqlite
Descriptors: yes
Encrypted: no
HD (hd seed available): yes
Keypool Size: 8000
Transactions: 0
Address Book: 0
</details>
Backwards compatibility: command lines where a dash-prefixed argument follows a non-dash argument will now be parsed as an option (if recognized) or an error (if not). Previously such arguments were silently collected as positional args. The -- separator can be used to pass arguments that begin with a dash as positional args.