qBittorrent
cmdoptions.cpp File Reference
#include "cmdoptions.h"
#include <cstdio>
#include <QDebug>
#include <QFileInfo>
#include <QProcessEnvironment>
#include <QTextStream>
#include "base/global.h"
#include "base/utils/misc.h"
#include "base/utils/string.h"
#include "gui/utils.h"
Include dependency graph for cmdoptions.cpp:

Go to the source code of this file.

Classes

class  anonymous_namespace{cmdoptions.cpp}::Option
 
class  anonymous_namespace{cmdoptions.cpp}::BoolOption
 
struct  anonymous_namespace{cmdoptions.cpp}::StringOption
 
class  anonymous_namespace{cmdoptions.cpp}::IntOption
 
class  anonymous_namespace{cmdoptions.cpp}::TriStateBoolOption
 

Namespaces

 anonymous_namespace{cmdoptions.cpp}
 

Functions

bool anonymous_namespace{cmdoptions.cpp}::operator== (const QString &s, const BoolOption &o)
 
bool anonymous_namespace{cmdoptions.cpp}::operator== (const QString &s, const StringOption &o)
 
bool anonymous_namespace{cmdoptions.cpp}::operator== (const QString &s, const IntOption &o)
 
bool anonymous_namespace{cmdoptions.cpp}::operator== (const QString &s, const TriStateBoolOption &o)
 
QBtCommandLineParameters parseCommandLine (const QStringList &args)
 
QString wrapText (const QString &text, int initialIndentation=USAGE_TEXT_COLUMN, int wrapAtColumn=WRAP_AT_COLUMN)
 
QString makeUsage (const QString &prgName)
 
void displayUsage (const QString &prgName)
 

Variables

const int anonymous_namespace{cmdoptions.cpp}::USAGE_INDENTATION = 4
 
const int anonymous_namespace{cmdoptions.cpp}::USAGE_TEXT_COLUMN = 31
 
const int anonymous_namespace{cmdoptions.cpp}::WRAP_AT_COLUMN = 80
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::SHOW_HELP_OPTION {"help", 'h'}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::SHOW_VERSION_OPTION {"version", 'v'}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::NO_SPLASH_OPTION {"no-splash"}
 
constexpr const IntOption anonymous_namespace{cmdoptions.cpp}::WEBUI_PORT_OPTION {"webui-port"}
 
constexpr const StringOption anonymous_namespace{cmdoptions.cpp}::PROFILE_OPTION {"profile"}
 
constexpr const StringOption anonymous_namespace{cmdoptions.cpp}::CONFIGURATION_OPTION {"configuration"}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::RELATIVE_FASTRESUME {"relative-fastresume"}
 
constexpr const StringOption anonymous_namespace{cmdoptions.cpp}::SAVE_PATH_OPTION {"save-path"}
 
constexpr const TriStateBoolOption anonymous_namespace{cmdoptions.cpp}::PAUSED_OPTION {"add-paused", true}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::SKIP_HASH_CHECK_OPTION {"skip-hash-check"}
 
constexpr const StringOption anonymous_namespace{cmdoptions.cpp}::CATEGORY_OPTION {"category"}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::SEQUENTIAL_OPTION {"sequential"}
 
constexpr const BoolOption anonymous_namespace{cmdoptions.cpp}::FIRST_AND_LAST_OPTION {"first-and-last"}
 
constexpr const TriStateBoolOption anonymous_namespace{cmdoptions.cpp}::SKIP_DIALOG_OPTION {"skip-dialog", true}
 

Function Documentation

◆ displayUsage()

void displayUsage ( const QString &  prgName)

Definition at line 582 of file cmdoptions.cpp.

583 {
584 #if defined(Q_OS_WIN) && !defined(DISABLE_GUI)
585  QMessageBox msgBox(QMessageBox::Information, QObject::tr("Help"), makeUsage(prgName), QMessageBox::Ok);
586  msgBox.show(); // Need to be shown or to moveToCenter does not work
587  msgBox.move(Utils::Gui::screenCenter(&msgBox));
588  msgBox.exec();
589 #else
590  printf("%s\n", qUtf8Printable(makeUsage(prgName)));
591 #endif
592 }
QString makeUsage(const QString &prgName)
Definition: cmdoptions.cpp:523
QPoint screenCenter(const QWidget *w)
Definition: utils.cpp:119

References makeUsage(), and Utils::Gui::screenCenter().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ makeUsage()

QString makeUsage ( const QString &  prgName)

Definition at line 523 of file cmdoptions.cpp.

524 {
525  QString text;
526  QTextStream stream(&text, QIODevice::WriteOnly);
527  QString indentation = QString(USAGE_INDENTATION, ' ');
528 
529  stream << QObject::tr("Usage:") << '\n'
530  << indentation << prgName << QLatin1String(" [options] [(<filename> | <url>)...]") << '\n'
531 
532  << QObject::tr("Options:") << '\n'
533 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
534  << SHOW_VERSION_OPTION.usage() << wrapText(QObject::tr("Display program version and exit")) << '\n'
535 #endif
536  << SHOW_HELP_OPTION.usage() << wrapText(QObject::tr("Display this help message and exit")) << '\n'
537  << WEBUI_PORT_OPTION.usage(QObject::tr("port"))
538  << wrapText(QObject::tr("Change the Web UI port"))
539  << '\n'
540 #ifndef DISABLE_GUI
541  << NO_SPLASH_OPTION.usage() << wrapText(QObject::tr("Disable splash screen")) << '\n'
542 #elif !defined(Q_OS_WIN)
543  << DAEMON_OPTION.usage() << wrapText(QObject::tr("Run in daemon-mode (background)")) << '\n'
544 #endif
545  //: Use appropriate short form or abbreviation of "directory"
546  << PROFILE_OPTION.usage(QObject::tr("dir"))
547  << wrapText(QObject::tr("Store configuration files in <dir>")) << '\n'
548  << CONFIGURATION_OPTION.usage(QObject::tr("name"))
549  << wrapText(QObject::tr("Store configuration files in directories qBittorrent_<name>")) << '\n'
551  << wrapText(QObject::tr("Hack into libtorrent fastresume files and make file paths relative "
552  "to the profile directory")) << '\n'
553  << Option::padUsageText(QObject::tr("files or URLs"))
554  << wrapText(QObject::tr("Download the torrents passed by the user")) << '\n'
555  << '\n'
556 
557  << wrapText(QObject::tr("Options when adding new torrents:"), 0) << '\n'
558  << SAVE_PATH_OPTION.usage(QObject::tr("path")) << wrapText(QObject::tr("Torrent save path")) << '\n'
559  << PAUSED_OPTION.usage() << wrapText(QObject::tr("Add torrents as started or paused")) << '\n'
560  << SKIP_HASH_CHECK_OPTION.usage() << wrapText(QObject::tr("Skip hash check")) << '\n'
561  << CATEGORY_OPTION.usage(QObject::tr("name"))
562  << wrapText(QObject::tr("Assign torrents to category. If the category doesn't exist, it will be "
563  "created.")) << '\n'
564  << SEQUENTIAL_OPTION.usage() << wrapText(QObject::tr("Download files in sequential order")) << '\n'
566  << wrapText(QObject::tr("Download first and last pieces first")) << '\n'
568  << wrapText(QObject::tr("Specify whether the \"Add New Torrent\" dialog opens when adding a "
569  "torrent.")) << '\n'
570  << '\n'
571 
572  << wrapText(QObject::tr("Option values may be supplied via environment variables. For option named "
573  "'parameter-name', environment variable name is 'QBT_PARAMETER_NAME' (in upper "
574  "case, '-' replaced with '_'). To pass flag values, set the variable to '1' or "
575  "'TRUE'. For example, to disable the splash screen: "), 0) << "\n"
576  << QLatin1String("QBT_NO_SPLASH=1 ") << prgName << '\n'
577  << wrapText(QObject::tr("Command line parameters take precedence over environment variables"), 0) << '\n';
578 
579  return text;
580 }
QString usage(const QString &valueName) const
Definition: cmdoptions.cpp:172
QString wrapText(const QString &text, int initialIndentation=USAGE_TEXT_COLUMN, int wrapAtColumn=WRAP_AT_COLUMN)
Definition: cmdoptions.cpp:501
constexpr const StringOption CATEGORY_OPTION
Definition: cmdoptions.cpp:333
constexpr const StringOption SAVE_PATH_OPTION
Definition: cmdoptions.cpp:330
constexpr const TriStateBoolOption SKIP_DIALOG_OPTION
Definition: cmdoptions.cpp:336
constexpr const BoolOption NO_SPLASH_OPTION
Definition: cmdoptions.cpp:324
constexpr const BoolOption SHOW_VERSION_OPTION
Definition: cmdoptions.cpp:320
constexpr const BoolOption RELATIVE_FASTRESUME
Definition: cmdoptions.cpp:329
constexpr const StringOption PROFILE_OPTION
Definition: cmdoptions.cpp:327
constexpr const IntOption WEBUI_PORT_OPTION
Definition: cmdoptions.cpp:326
constexpr const BoolOption SEQUENTIAL_OPTION
Definition: cmdoptions.cpp:334
constexpr const BoolOption FIRST_AND_LAST_OPTION
Definition: cmdoptions.cpp:335
constexpr const BoolOption SKIP_HASH_CHECK_OPTION
Definition: cmdoptions.cpp:332
constexpr const BoolOption SHOW_HELP_OPTION
Definition: cmdoptions.cpp:319
constexpr const StringOption CONFIGURATION_OPTION
Definition: cmdoptions.cpp:328
constexpr const TriStateBoolOption PAUSED_OPTION
Definition: cmdoptions.cpp:331
QString usage(const QString &valueName) const
Definition: cmdoptions.cpp:172

References anonymous_namespace{cmdoptions.cpp}::CATEGORY_OPTION, anonymous_namespace{cmdoptions.cpp}::CONFIGURATION_OPTION, anonymous_namespace{cmdoptions.cpp}::FIRST_AND_LAST_OPTION, anonymous_namespace{cmdoptions.cpp}::NO_SPLASH_OPTION, anonymous_namespace{cmdoptions.cpp}::PAUSED_OPTION, anonymous_namespace{cmdoptions.cpp}::PROFILE_OPTION, anonymous_namespace{cmdoptions.cpp}::RELATIVE_FASTRESUME, anonymous_namespace{cmdoptions.cpp}::SAVE_PATH_OPTION, anonymous_namespace{cmdoptions.cpp}::SEQUENTIAL_OPTION, anonymous_namespace{cmdoptions.cpp}::SHOW_HELP_OPTION, anonymous_namespace{cmdoptions.cpp}::SHOW_VERSION_OPTION, anonymous_namespace{cmdoptions.cpp}::SKIP_DIALOG_OPTION, anonymous_namespace{cmdoptions.cpp}::SKIP_HASH_CHECK_OPTION, anonymous_namespace{cmdoptions.cpp}::BoolOption::usage(), anonymous_namespace{cmdoptions.cpp}::TriStateBoolOption::usage(), anonymous_namespace{cmdoptions.cpp}::StringOption::usage(), anonymous_namespace{cmdoptions.cpp}::IntOption::usage(), anonymous_namespace{cmdoptions.cpp}::USAGE_INDENTATION, anonymous_namespace{cmdoptions.cpp}::WEBUI_PORT_OPTION, and wrapText().

Referenced by displayUsage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseCommandLine()

QBtCommandLineParameters parseCommandLine ( const QStringList &  args)

Definition at line 399 of file cmdoptions.cpp.

400 {
401  QBtCommandLineParameters result {QProcessEnvironment::systemEnvironment()};
402 
403  for (int i = 1; i < args.count(); ++i)
404  {
405  const QString &arg = args[i];
406 
407  if ((arg.startsWith("--") && !arg.endsWith(".torrent"))
408  || (arg.startsWith('-') && (arg.size() == 2)))
409  {
410  // Parse known parameters
411  if (arg == SHOW_HELP_OPTION)
412  {
413  result.showHelp = true;
414  }
415 #if !defined(Q_OS_WIN) || defined(DISABLE_GUI)
416  else if (arg == SHOW_VERSION_OPTION)
417  {
418  result.showVersion = true;
419  }
420 #endif
421  else if (arg == WEBUI_PORT_OPTION)
422  {
423  result.webUiPort = WEBUI_PORT_OPTION.value(arg);
424  if ((result.webUiPort < 1) || (result.webUiPort > 65535))
425  throw CommandLineParameterError(QObject::tr("%1 must specify a valid port (1 to 65535).")
426  .arg(QLatin1String("--webui-port")));
427  }
428 #ifndef DISABLE_GUI
429  else if (arg == NO_SPLASH_OPTION)
430  {
431  result.noSplash = true;
432  }
433 #elif !defined(Q_OS_WIN)
434  else if (arg == DAEMON_OPTION)
435  {
436  result.shouldDaemonize = true;
437  }
438 #endif
439  else if (arg == PROFILE_OPTION)
440  {
441  result.profileDir = PROFILE_OPTION.value(arg);
442  }
443  else if (arg == RELATIVE_FASTRESUME)
444  {
445  result.relativeFastresumePaths = true;
446  }
447  else if (arg == CONFIGURATION_OPTION)
448  {
449  result.configurationName = CONFIGURATION_OPTION.value(arg);
450  }
451  else if (arg == SAVE_PATH_OPTION)
452  {
453  result.savePath = SAVE_PATH_OPTION.value(arg);
454  }
455  else if (arg == PAUSED_OPTION)
456  {
457  result.addPaused = PAUSED_OPTION.value(arg);
458  }
459  else if (arg == SKIP_HASH_CHECK_OPTION)
460  {
461  result.skipChecking = true;
462  }
463  else if (arg == CATEGORY_OPTION)
464  {
465  result.category = CATEGORY_OPTION.value(arg);
466  }
467  else if (arg == SEQUENTIAL_OPTION)
468  {
469  result.sequential = true;
470  }
471  else if (arg == FIRST_AND_LAST_OPTION)
472  {
473  result.firstLastPiecePriority = true;
474  }
475  else if (arg == SKIP_DIALOG_OPTION)
476  {
477  result.skipDialog = SKIP_DIALOG_OPTION.value(arg);
478  }
479  else
480  {
481  // Unknown argument
482  result.unknownParameter = arg;
483  break;
484  }
485  }
486  else
487  {
488  QFileInfo torrentPath;
489  torrentPath.setFile(arg);
490 
491  if (torrentPath.exists())
492  result.torrents += torrentPath.absoluteFilePath();
493  else
494  result.torrents += arg;
495  }
496  }
497 
498  return result;
499 }
std::optional< bool > value(const QString &arg) const
Definition: cmdoptions.cpp:257
args
Definition: tstool.py:153
QString value(const QString &arg) const
Definition: cmdoptions.cpp:156

References tstool::args, anonymous_namespace{cmdoptions.cpp}::CATEGORY_OPTION, anonymous_namespace{cmdoptions.cpp}::CONFIGURATION_OPTION, anonymous_namespace{cmdoptions.cpp}::FIRST_AND_LAST_OPTION, anonymous_namespace{cmdoptions.cpp}::NO_SPLASH_OPTION, anonymous_namespace{cmdoptions.cpp}::PAUSED_OPTION, anonymous_namespace{cmdoptions.cpp}::PROFILE_OPTION, anonymous_namespace{cmdoptions.cpp}::RELATIVE_FASTRESUME, anonymous_namespace{cmdoptions.cpp}::SAVE_PATH_OPTION, anonymous_namespace{cmdoptions.cpp}::SEQUENTIAL_OPTION, anonymous_namespace{cmdoptions.cpp}::SHOW_HELP_OPTION, anonymous_namespace{cmdoptions.cpp}::SHOW_VERSION_OPTION, anonymous_namespace{cmdoptions.cpp}::SKIP_DIALOG_OPTION, anonymous_namespace{cmdoptions.cpp}::SKIP_HASH_CHECK_OPTION, anonymous_namespace{cmdoptions.cpp}::StringOption::value(), anonymous_namespace{cmdoptions.cpp}::IntOption::value(), anonymous_namespace{cmdoptions.cpp}::TriStateBoolOption::value(), and anonymous_namespace{cmdoptions.cpp}::WEBUI_PORT_OPTION.

Here is the call graph for this function:

◆ wrapText()

QString wrapText ( const QString &  text,
int  initialIndentation = USAGE_TEXT_COLUMN,
int  wrapAtColumn = WRAP_AT_COLUMN 
)

Definition at line 501 of file cmdoptions.cpp.

502 {
503  QStringList words = text.split(' ');
504  QStringList lines = {words.first()};
505  int currentLineMaxLength = wrapAtColumn - initialIndentation;
506 
507  for (const QString &word : asConst(words.mid(1)))
508  {
509  if (lines.last().length() + word.length() + 1 < currentLineMaxLength)
510  {
511  lines.last().append(' ' + word);
512  }
513  else
514  {
515  lines.append(QString(initialIndentation, ' ') + word);
516  currentLineMaxLength = wrapAtColumn;
517  }
518  }
519 
520  return lines.join('\n');
521 }
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42

References asConst().

Referenced by makeUsage().

Here is the call graph for this function:
Here is the caller graph for this function: