Skip to content

Commit afa081a

Browse files
committed
Merge #742: Exit and show error if unrecognized command line args are present
51e4dc4 gui: Show error if unrecognized command line args are present (John Moffett) Pull request description: Fixes #741 Starting bitcoin-qt with non-hyphen ("-") arguments causes it to silently ignore any later valid options. For instance, invoking `bitcoin-qt -server=1 foo -regtest` on a fresh install will run `mainnet` instead of `regtest`. This change makes the client exit with an error message if any such "loose" arguments are encountered. This mirrors how `bitcoind` handles it: https://github.com/bitcoin/bitcoin/blob/c6287faae4c0e705a9258a340dfcf548906f12af/src/bitcoind.cpp#L127-L132 However, BIP-21 `bitcoin:` payment URIs are still allowed, but only if they're not followed by any additional options. ACKs for top commit: maflcko: lgtm ACK 51e4dc4 hernanmarino: tested ACK 51e4dc4 pablomartin4btc: tACK 51e4dc4 hebasto: ACK 51e4dc4, I have reviewed the code and it looks OK. Tree-SHA512: 3997a7a9a747314f13e118aee63e8679e00ed832d9c6f115559a4c39c9c4091572207c60e362cb4c19fc8da980d4b0b040050aa70c5ef84a855cb7e3568bbf13
2 parents d53400e + 51e4dc4 commit afa081a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/qt/bitcoin.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,34 @@ int GuiMain(int argc, char* argv[])
547547
return EXIT_FAILURE;
548548
}
549549

550+
// Error out when loose non-argument tokens are encountered on command line
551+
// However, allow BIP-21 URIs only if no options follow
552+
bool payment_server_token_seen = false;
553+
for (int i = 1; i < argc; i++) {
554+
QString arg(argv[i]);
555+
bool invalid_token = !arg.startsWith("-");
556+
#ifdef ENABLE_WALLET
557+
if (arg.startsWith(BITCOIN_IPC_PREFIX, Qt::CaseInsensitive)) {
558+
invalid_token &= false;
559+
payment_server_token_seen = true;
560+
}
561+
#endif
562+
if (payment_server_token_seen && arg.startsWith("-")) {
563+
InitError(Untranslated(strprintf("Options ('%s') cannot follow a BIP-21 payment URI", argv[i])));
564+
QMessageBox::critical(nullptr, PACKAGE_NAME,
565+
// message cannot be translated because translations have not been initialized
566+
QString::fromStdString("Options ('%1') cannot follow a BIP-21 payment URI").arg(QString::fromStdString(argv[i])));
567+
return EXIT_FAILURE;
568+
}
569+
if (invalid_token) {
570+
InitError(Untranslated(strprintf("Command line contains unexpected token '%s', see bitcoin-qt -h for a list of options.", argv[i])));
571+
QMessageBox::critical(nullptr, PACKAGE_NAME,
572+
// message cannot be translated because translations have not been initialized
573+
QString::fromStdString("Command line contains unexpected token '%1', see bitcoin-qt -h for a list of options.").arg(QString::fromStdString(argv[i])));
574+
return EXIT_FAILURE;
575+
}
576+
}
577+
550578
// Now that the QApplication is setup and we have parsed our parameters, we can set the platform style
551579
app.setupPlatformStyle();
552580

src/qt/paymentserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class QLocalServer;
5454
class QUrl;
5555
QT_END_NAMESPACE
5656

57+
extern const QString BITCOIN_IPC_PREFIX;
58+
5759
class PaymentServer : public QObject
5860
{
5961
Q_OBJECT

0 commit comments

Comments
 (0)