@@ -402,18 +402,25 @@ class RPCHelpMan
402
402
403
403
UniValue HandleRequest (const JSONRPCRequest& request) const ;
404
404
/* *
405
- * Helper to get a request argument.
406
- * This function only works during m_fun(), i.e. it should only be used in
407
- * RPC method implementations. The helper internally checks whether the
408
- * user-passed argument isNull() and parses (from JSON) and returns the
409
- * user-passed argument, or the default value derived from the RPCArg
410
- * documentation, or a falsy value if no default was given.
405
+ * @brief Helper to get a required or default-valued request argument.
411
406
*
412
- * Use Arg<Type>(i) to get the argument or its default value. Otherwise,
413
- * use MaybeArg<Type>(i) to get the optional argument or a falsy value .
407
+ * Use this function when the argument is required or when it has a default value. If the
408
+ * argument is optional and may not be provided, use MaybeArg instead .
414
409
*
415
- * The Type passed to this helper must match the corresponding
416
- * RPCArg::Type.
410
+ * This function only works during m_fun(), i.e., it should only be used in
411
+ * RPC method implementations. It internally checks whether the user-passed
412
+ * argument isNull() and parses (from JSON) and returns the user-passed argument,
413
+ * or the default value derived from the RPCArg documentation.
414
+ *
415
+ * There are two overloads of this function:
416
+ * - Use Arg<Type>(size_t i) to get the argument (or the default value) by index.
417
+ * - Use Arg<Type>(const std::string& key) to get the argument (or the default value) by key.
418
+ *
419
+ * The Type passed to this helper must match the corresponding RPCArg::Type.
420
+ *
421
+ * @return The value of the RPC argument (or the default value) cast to type Type.
422
+ *
423
+ * @see MaybeArg for handling optional arguments without default values.
417
424
*/
418
425
template <typename R>
419
426
auto Arg (size_t i) const
@@ -427,6 +434,34 @@ class RPCHelpMan
427
434
return ArgValue<const R&>(i);
428
435
}
429
436
}
437
+ template <typename R>
438
+ auto Arg (std::string_view key) const
439
+ {
440
+ return Arg<R>(GetParamIndex (key));
441
+ }
442
+ /* *
443
+ * @brief Helper to get an optional request argument.
444
+ *
445
+ * Use this function when the argument is optional and does not have a default value. If the
446
+ * argument is required or has a default value, use Arg instead.
447
+ *
448
+ * This function only works during m_fun(), i.e., it should only be used in
449
+ * RPC method implementations. It internally checks whether the user-passed
450
+ * argument isNull() and parses (from JSON) and returns the user-passed argument,
451
+ * or a falsy value if no argument was passed.
452
+ *
453
+ * There are two overloads of this function:
454
+ * - Use MaybeArg<Type>(size_t i) to get the optional argument by index.
455
+ * - Use MaybeArg<Type>(const std::string& key) to get the optional argument by key.
456
+ *
457
+ * The Type passed to this helper must match the corresponding RPCArg::Type.
458
+ *
459
+ * @return For integral and floating-point types, a std::optional<Type> is returned.
460
+ * For other types, a Type* pointer to the argument is returned. If the
461
+ * argument is not provided, std::nullopt or a null pointer is returned.
462
+ *
463
+ * @see Arg for handling arguments that are required or have a default value.
464
+ */
430
465
template <typename R>
431
466
auto MaybeArg (size_t i) const
432
467
{
@@ -439,6 +474,11 @@ class RPCHelpMan
439
474
return ArgValue<const R*>(i);
440
475
}
441
476
}
477
+ template <typename R>
478
+ auto MaybeArg (std::string_view key) const
479
+ {
480
+ return MaybeArg<R>(GetParamIndex (key));
481
+ }
442
482
std::string ToString () const ;
443
483
/* * Return the named args that need to be converted from string to another JSON type */
444
484
UniValue GetArgMap () const ;
@@ -458,6 +498,8 @@ class RPCHelpMan
458
498
mutable const JSONRPCRequest* m_req{nullptr }; // A pointer to the request for the duration of m_fun()
459
499
template <typename R>
460
500
R ArgValue (size_t i) const ;
501
+ // ! Return positional index of a parameter using its name as key.
502
+ size_t GetParamIndex (std::string_view key) const ;
461
503
};
462
504
463
505
/* *
0 commit comments