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:
0Options:
1 -dumpfile=<file name>
2 When used with 'dump', writes out the records to this file. When used
3 with 'createfromdump', loads the records into a new wallet.
4 ...
5
6Commands:
7 createfromdump
8 Create new wallet file from dumped records
9
10 dump
11 Print out all of the wallet key-value records
After:
0Commands:
1 createfromdump
2 Create new wallet file from dumped records
3
4 -dumpfile=<file name>
5 When used with 'dump', writes out the records to this file. When
6 used with 'createfromdump', loads the records into a new wallet.
7
8 dump
9 Print out all of the wallet key-value records
10
11 -dumpfile=<file name>
12 When used with 'dump', writes out the records to this file. When
13 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:
0 if (args.IsArgSet("-dumpfile") && command != "dump" && command != "createfromdump") {
1 tfm::format(std::cerr, "The -dumpfile option can only be used with the \"dump\" and \"createfromdump\" commands.\n");
2 return false;
3 }
After:
0 std::vector<std::string> details;
1 if (!args.CheckCommandOptions(command, &details)) {
2 tfm::format(std::cerr, "Error: Invalid arguments provided:\n%s\n", util::MakeUnorderedList(details));
3 return false;
4 }
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.