You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge #20715: util: Add ArgsManager::GetCommand() and use it in bitcoin-wallet
fa61b9d util: Add ArgsManager::GetCommand() and use it in bitcoin-wallet (MarcoFalke)
7777105 refactor: Move all command dependend checks to ExecuteWalletToolFunc (MarcoFalke)
fa06bce test: Add tests (MarcoFalke)
fac05cc wallet: [refactor] Pass ArgsManager to WalletAppInit (MarcoFalke)
Pull request description:
This not only moves the parsing responsibility out from the wallet tool, but it also makes it easier to implement bitcoin-util #19937
Fixes: #20902
ACKs for top commit:
ajtowns:
ACK fa61b9d
fjahr:
Code review ACK fa61b9d
Tree-SHA512: 79622b806e8bf9dcd0dc24a8a6687345710df57720992e83a41cd8d6762a6dc112044ebc58fcf6e8fbf45de29a79b04873c5b8c2494a1eaaf902a2884703e47b
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
34
34
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
argsman.AddArg("create", "Create new wallet file", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
38
-
argsman.AddArg("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
39
-
argsman.AddArg("dump", "Print out all of the wallet key-value records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
40
-
argsman.AddArg("createfromdump", "Create new wallet file from dumped records", ArgsManager::ALLOW_ANY, OptionsCategory::COMMANDS);
argsman.AddCommand("create", "Create new wallet file", OptionsCategory::COMMANDS);
38
+
argsman.AddCommand("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", OptionsCategory::COMMANDS);
39
+
argsman.AddCommand("dump", "Print out all of the wallet key-value records", OptionsCategory::COMMANDS);
40
+
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records", OptionsCategory::COMMANDS);
41
41
}
42
42
43
-
staticboolWalletAppInit(int argc, char* argv[])
43
+
staticboolWalletAppInit(ArgsManager& args, int argc, char* argv[])
44
44
{
45
-
SetupWalletToolArgs(gArgs);
45
+
SetupWalletToolArgs(args);
46
46
std::string error_message;
47
-
if (!gArgs.ParseParameters(argc, argv, error_message)) {
47
+
if (!args.ParseParameters(argc, argv, error_message)) {
48
48
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message);
49
49
returnfalse;
50
50
}
51
-
if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
51
+
if (argc < 2 || HelpRequested(args) || args.IsArgSet("-version")) {
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
70
+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
71
71
returnfalse;
72
72
}
73
73
// Check for chain settings (Params() calls are only valid after this clause)
74
-
SelectParams(gArgs.GetChainName());
74
+
SelectParams(args.GetChainName());
75
75
76
76
returntrue;
77
77
}
78
78
79
79
intmain(int argc, char* argv[])
80
80
{
81
+
ArgsManager& args = gArgs;
81
82
#ifdef WIN32
82
83
util::WinCmdLineArgs winArgs;
83
84
std::tie(argc, argv) = winArgs.get();
84
85
#endif
85
86
SetupEnvironment();
86
87
RandomInit();
87
88
try {
88
-
if (!WalletAppInit(argc, argv)) return EXIT_FAILURE;
89
+
if (!WalletAppInit(args, argc, argv)) return EXIT_FAILURE;
89
90
} catch (const std::exception& e) {
90
91
PrintExceptionContinue(&e, "WalletAppInit()");
91
92
return EXIT_FAILURE;
@@ -94,33 +95,19 @@ int main(int argc, char* argv[])
94
95
return EXIT_FAILURE;
95
96
}
96
97
97
-
std::string method {};
98
-
for(int i = 1; i < argc; ++i) {
99
-
if (!IsSwitchChar(argv[i][0])) {
100
-
if (!method.empty()) {
101
-
tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method, argv[i]);
102
-
return EXIT_FAILURE;
103
-
}
104
-
method = argv[i];
105
-
}
106
-
}
107
-
108
-
if (method.empty()) {
98
+
constauto command = args.GetCommand();
99
+
if (!command) {
109
100
tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
110
101
return EXIT_FAILURE;
111
102
}
112
-
113
-
// A name must be provided when creating a file
114
-
if (method == "create" && !gArgs.IsArgSet("-wallet")) {
115
-
tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
103
+
if (command->args.size() != 0) {
104
+
tfm::format(std::cerr, "Error: Additional arguments provided (%s). Methods do not take arguments. Please refer to `-help`.\n", Join(command->args, ", "));
116
105
return EXIT_FAILURE;
117
106
}
118
107
119
-
std::string name = gArgs.GetArg("-wallet", "");
120
-
121
108
ECCVerifyHandle globalVerifyHandle;
122
109
ECC_Start();
123
-
if (!WalletTool::ExecuteWalletToolFunc(gArgs, method, name)) {
110
+
if (!WalletTool::ExecuteWalletToolFunc(args, command->command)) {
self.assert_raises_tool_error('Error: two methods provided (info and create). Only one method should be provided.', 'info', 'create')
188
+
self.assert_raises_tool_error("Error parsing command line arguments: Invalid command 'help'", 'help')
189
+
self.assert_raises_tool_error('Error: Additional arguments provided (create). Methods do not take arguments. Please refer to `-help`.', 'info', 'create')
190
190
self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
191
+
self.assert_raises_tool_error('No method provided. Run `bitcoin-wallet -help` for valid methods.')
192
+
self.assert_raises_tool_error('Wallet name must be provided when creating a new wallet.', 'create')
0 commit comments