re: #19762 (review)
Is there a particular reason to allow keyword arguments before positional arguments? Even though from the above I understand there is currently no technical ambiguity, I think disallowing it (such as e.g. enforced in Python) makes for a more clear interface. Users cannot be confused about whether or not a positional argument after a keyword argument gets bumped up or not - it is simply not allowed, and the server can trivially make that clear in its response. Additionally, it’ll be easier to become more lenient in the future than to revert this behaviour, so I would be careful about allowing it now if there is no clear benefit.
If you think the current behaviour is warranted, perhaps that should be documented more clearly in JSON-RPC-interface.md
?
In the JSON-RPC request, keyword arguments are not really allowed before or after positional arguments because JSON objects are supposed to be “unordered”, so there should not be a concept of before or after. The request object contains a params
object, and named arguments are fields of that object. By convention after this PR, the params
object can have an extra field called args
which has a list of initial positional arguments. I don’t think it would be good to enforce any particular field order in the params
JSON object, because it would break potential clients sending requests that have different field orders but are otherwise unambiguous. The RFC even says “implementations whose behavior does not depend on member ordering” are more interoperable, so that is the goal here. It’s why the server doesn’t care about the field order and why JSON-RPC-interface.md is not recommending any field order.
On the client side, it is up to individual clients to choose what syntax to allow and how to generate JSON-RPC requests from that syntax.
In the python RPC client, we’re taking advantage of python function call syntax, and because python syntax only allows named arguments after unnamed ones, that is what our python RPC calls allow.
In the bitcoin-cli
client, things are different because command line conventions are not the same as function calling conventions. The most common and user-friendly command line tools tend to not put restrictions on where their named parameters (usually options beginning with -
or --
) are placed or what order they need to be specified. Even command line tools like git that do restrict where options go usually require options to be specified before positional arguments, not after so I think bitcoin-cli
requiring options to be specified last would give it a uniquely awkward and unconventional interface. Personally, I think bitcoin-cli
should try to act like other command line tools that are pleasant to use, and allow options to be placed anywhere in any order. There are lots of hurdles we could place in front of command line users to slow them down and catch potential mistakes, but I think we should avoid going out of our way to add a hurdle that doesn’t need to exist and doesn’t make usage more safe or convenient in a specific way.
I think allowing options in any order is just one way this PR can make the command line easier to use. Followup #26485 builds on this and adds support for passing name-only options (similar to https://peps.python.org/pep-3102/). This PR can also combine nicely with #20273 (as described #19762 (comment)) to support the python-style syntax we both like in bitcoin-cli
.