Adds the ability to link particular options to one or more (OptionsCategory::COMMANDS) commands, and uses this feature
in bitcoin-wallet. Separates out the help information for these command-specific options (duplicating it if an option applies to multiple commands), and provides a function for checking at runtime if some options have been specified by the user that only apply to other commands.
Motivation
Currently, ArgsManager supports commands like bitcoin-wallet dump but while some of the options are command-specific (like -dumpfile), ArgsManager itself doesn't know that. As a result, -dumpfile is listed in the global help rather than under the relevant commands, and if you use -dumpfile with a different command that doesn't support it, ArgsManager cannot automatically report that as an error, resulting in the commands that don't support the option having to have error-handling specific to all the options they don't support.
Changes
Help output moves command-specific options under their associated commands:
Before:
Options:
-dumpfile=<file name>
When used with 'dump', writes out the records to this file. When used
with 'createfromdump', loads the records into a new wallet.
...
Commands:
createfromdump
Create new wallet file from dumped records
dump
Print out all of the wallet key-value records
After:
Commands:
createfromdump
Create new wallet file from dumped records
-dumpfile=<file name>
When used with 'dump', writes out the records to this file. When
used with 'createfromdump', loads the records into a new wallet.
dump
Print out all of the wallet key-value records
-dumpfile=<file name>
When used with 'dump', writes out the records to this file. When
used with 'createfromdump', loads the records into a new wallet.
Error messages are now generated automatically by ArgsManager rather than ad-hoc wallet code for each option:
Before:
if (args.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
return false;
}
After:
std::vector<std::string> details;
if (!args.CheckCommandOptions(command, &details)) {
tfm::format(std::cerr, "Error: Invalid arguments provided:\n%s\n", util::MakeUnorderedList(details));
return false;
}
Limitations
- If an option applies to multiple commands, it shares the same help text. There's no way to provide per-command descriptions.
- Option parsing rules are unchanged — options still cannot appear after the command.