Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

qcommandlineoption.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Laszlo Papp <lpapp@kde.org>
  4. ** Copyright (C) 2013 David Faure <faure@kde.org>
  5. ** Contact: http://www.qt.io/licensing/
  6. **
  7. ** This file is part of the QtCore module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL21$
  10. ** Commercial License Usage
  11. ** Licensees holding valid commercial Qt licenses may use this file in
  12. ** accordance with the commercial license agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and The Qt Company. For licensing terms
  15. ** and conditions see http://www.qt.io/terms-conditions. For further
  16. ** information use the contact form at http://www.qt.io/contact-us.
  17. **
  18. ** GNU Lesser General Public License Usage
  19. ** Alternatively, this file may be used under the terms of the GNU Lesser
  20. ** General Public License version 2.1 or version 3 as published by the Free
  21. ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
  22. ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
  23. ** following information to ensure the GNU Lesser General Public License
  24. ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
  25. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  26. **
  27. ** As a special exception, The Qt Company gives you certain additional
  28. ** rights. These rights are described in The Qt Company LGPL Exception
  29. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  30. **
  31. ** $QT_END_LICENSE$
  32. **
  33. ****************************************************************************/
  34. #include "qcommandlineoption.h"
  35. #include "qset.h"
  36. QT_BEGIN_NAMESPACE
  37. class QCommandLineOptionPrivate : public QSharedData
  38. {
  39. public:
  40. inline QCommandLineOptionPrivate()
  41. { }
  42. void setNames(const QStringList &nameList);
  43. //! The list of names used for this option.
  44. QStringList names;
  45. //! The documentation name for the value, if one is expected
  46. //! Example: "-o <file>" means valueName == "file"
  47. QString valueName;
  48. //! The description used for this option.
  49. QString description;
  50. //! The list of default values used for this option.
  51. QStringList defaultValues;
  52. };
  53. /*!
  54. \since 5.2
  55. \class QCommandLineOption
  56. \brief The QCommandLineOption class defines a possible command-line option.
  57. \inmodule QtCore
  58. \ingroup shared
  59. \ingroup tools
  60. This class is used to describe an option on the command line. It allows
  61. different ways of defining the same option with multiple aliases possible.
  62. It is also used to describe how the option is used - it may be a flag (e.g. \c{-v})
  63. or take a value (e.g. \c{-o file}).
  64. Examples:
  65. \snippet code/src_corelib_tools_qcommandlineoption.cpp 0
  66. \sa QCommandLineParser
  67. */
  68. /*!
  69. \fn QCommandLineOption &QCommandLineOption::operator=(QCommandLineOption &&other)
  70. Move-assigns \a other to this QCommandLineOption instance.
  71. \since 5.2
  72. */
  73. /*!
  74. Constructs a command line option object with the name \a name.
  75. The name can be either short or long. If the name is one character in
  76. length, it is considered a short name. Option names must not be empty,
  77. must not start with a dash or a slash character, must not contain a \c{=}
  78. and cannot be repeated.
  79. \sa setDescription(), setValueName(), setDefaultValues()
  80. */
  81. QCommandLineOption::QCommandLineOption(const QString &name)
  82. : d(new QCommandLineOptionPrivate)
  83. {
  84. d->setNames(QStringList(name));
  85. }
  86. /*!
  87. Constructs a command line option object with the names \a names.
  88. This overload allows to set multiple names for the option, for instance
  89. \c{o} and \c{output}.
  90. The names can be either short or long. Any name in the list that is one
  91. character in length is a short name. Option names must not be empty,
  92. must not start with a dash or a slash character, must not contain a \c{=}
  93. and cannot be repeated.
  94. \sa setDescription(), setValueName(), setDefaultValues()
  95. */
  96. QCommandLineOption::QCommandLineOption(const QStringList &names)
  97. : d(new QCommandLineOptionPrivate)
  98. {
  99. d->setNames(names);
  100. }
  101. /*!
  102. Constructs a command line option object with the given arguments.
  103. The name of the option is set to \a name.
  104. The name can be either short or long. If the name is one character in
  105. length, it is considered a short name. Option names must not be empty,
  106. must not start with a dash or a slash character, must not contain a \c{=}
  107. and cannot be repeated.
  108. The description is set to \a description. It is customary to add a "."
  109. at the end of the description.
  110. In addition, the \a valueName can be set if the option expects a value.
  111. The default value for the option is set to \a defaultValue.
  112. In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
  113. and later, it no longer is and can be used for C++11-style uniform
  114. initialization:
  115. \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init
  116. \sa setDescription(), setValueName(), setDefaultValues()
  117. */
  118. QCommandLineOption::QCommandLineOption(const QString &name, const QString &description,
  119. const QString &valueName,
  120. const QString &defaultValue)
  121. : d(new QCommandLineOptionPrivate)
  122. {
  123. d->setNames(QStringList(name));
  124. setValueName(valueName);
  125. setDescription(description);
  126. setDefaultValue(defaultValue);
  127. }
  128. /*!
  129. Constructs a command line option object with the given arguments.
  130. This overload allows to set multiple names for the option, for instance
  131. \c{o} and \c{output}.
  132. The names of the option are set to \a names.
  133. The names can be either short or long. Any name in the list that is one
  134. character in length is a short name. Option names must not be empty,
  135. must not start with a dash or a slash character, must not contain a \c{=}
  136. and cannot be repeated.
  137. The description is set to \a description. It is customary to add a "."
  138. at the end of the description.
  139. In addition, the \a valueName can be set if the option expects a value.
  140. The default value for the option is set to \a defaultValue.
  141. In Qt versions before 5.4, this constructor was \c explicit. In Qt 5.4
  142. and later, it no longer is and can be used for C++11-style uniform
  143. initialization:
  144. \snippet code/src_corelib_tools_qcommandlineoption.cpp cxx11-init-list
  145. \sa setDescription(), setValueName(), setDefaultValues()
  146. */
  147. QCommandLineOption::QCommandLineOption(const QStringList &names, const QString &description,
  148. const QString &valueName,
  149. const QString &defaultValue)
  150. : d(new QCommandLineOptionPrivate)
  151. {
  152. d->setNames(names);
  153. setValueName(valueName);
  154. setDescription(description);
  155. setDefaultValue(defaultValue);
  156. }
  157. /*!
  158. Constructs a QCommandLineOption object that is a copy of the QCommandLineOption
  159. object \a other.
  160. \sa operator=()
  161. */
  162. QCommandLineOption::QCommandLineOption(const QCommandLineOption &other)
  163. : d(other.d)
  164. {
  165. }
  166. /*!
  167. Destroys the command line option object.
  168. */
  169. QCommandLineOption::~QCommandLineOption()
  170. {
  171. }
  172. /*!
  173. Makes a copy of the \a other object and assigns it to this QCommandLineOption
  174. object.
  175. */
  176. QCommandLineOption &QCommandLineOption::operator=(const QCommandLineOption &other)
  177. {
  178. d = other.d;
  179. return *this;
  180. }
  181. /*!
  182. \fn void QCommandLineOption::swap(QCommandLineOption &other)
  183. Swaps option \a other with this option. This operation is very
  184. fast and never fails.
  185. */
  186. /*!
  187. Returns the names set for this option.
  188. */
  189. QStringList QCommandLineOption::names() const
  190. {
  191. return d->names;
  192. }
  193. void QCommandLineOptionPrivate::setNames(const QStringList &nameList)
  194. {
  195. QStringList newNames;
  196. newNames.reserve(nameList.size());
  197. if (nameList.isEmpty())
  198. qWarning("QCommandLineOption: Options must have at least one name");
  199. foreach (const QString &name, nameList) {
  200. if (name.isEmpty()) {
  201. qWarning("QCommandLineOption: Option names cannot be empty");
  202. } else {
  203. const QChar c = name.at(0);
  204. if (c == QLatin1Char('-'))
  205. qWarning("QCommandLineOption: Option names cannot start with a '-'");
  206. else if (c == QLatin1Char('/'))
  207. qWarning("QCommandLineOption: Option names cannot start with a '/'");
  208. else if (name.contains(QLatin1Char('=')))
  209. qWarning("QCommandLineOption: Option names cannot contain a '='");
  210. else
  211. newNames.append(name);
  212. }
  213. }
  214. // commit
  215. names.swap(newNames);
  216. }
  217. /*!
  218. Sets the name of the expected value, for the documentation, to \a valueName.
  219. Options without a value assigned have a boolean-like behavior:
  220. either the user specifies --option or they don't.
  221. Options with a value assigned need to set a name for the expected value,
  222. for the documentation of the option in the help output. An option with names \c{o} and \c{output},
  223. and a value name of \c{file} will appear as \c{-o, --output <file>}.
  224. Call QCommandLineParser::value() if you expect the option to be present
  225. only once, and QCommandLineParser::values() if you expect that option
  226. to be present multiple times.
  227. \sa valueName()
  228. */
  229. void QCommandLineOption::setValueName(const QString &valueName)
  230. {
  231. d->valueName = valueName;
  232. }
  233. /*!
  234. Returns the name of the expected value.
  235. If empty, the option doesn't take a value.
  236. \sa setValueName()
  237. */
  238. QString QCommandLineOption::valueName() const
  239. {
  240. return d->valueName;
  241. }
  242. /*!
  243. Sets the description used for this option to \a description.
  244. It is customary to add a "." at the end of the description.
  245. The description is used by QCommandLineParser::showHelp().
  246. \sa description()
  247. */
  248. void QCommandLineOption::setDescription(const QString &description)
  249. {
  250. d->description = description;
  251. }
  252. /*!
  253. Returns the description set for this option.
  254. \sa setDescription()
  255. */
  256. QString QCommandLineOption::description() const
  257. {
  258. return d->description;
  259. }
  260. /*!
  261. Sets the default value used for this option to \a defaultValue.
  262. The default value is used if the user of the application does not specify
  263. the option on the command line.
  264. If \a defaultValue is empty, the option has no default values.
  265. \sa defaultValues() setDefaultValues()
  266. */
  267. void QCommandLineOption::setDefaultValue(const QString &defaultValue)
  268. {
  269. QStringList newDefaultValues;
  270. if (!defaultValue.isEmpty()) {
  271. newDefaultValues.reserve(1);
  272. newDefaultValues << defaultValue;
  273. }
  274. // commit:
  275. d->defaultValues.swap(newDefaultValues);
  276. }
  277. /*!
  278. Sets the list of default values used for this option to \a defaultValues.
  279. The default values are used if the user of the application does not specify
  280. the option on the command line.
  281. \sa defaultValues() setDefaultValue()
  282. */
  283. void QCommandLineOption::setDefaultValues(const QStringList &defaultValues)
  284. {
  285. d->defaultValues = defaultValues;
  286. }
  287. /*!
  288. Returns the default values set for this option.
  289. \sa setDefaultValues()
  290. */
  291. QStringList QCommandLineOption::defaultValues() const
  292. {
  293. return d->defaultValues;
  294. }
  295. QT_END_NAMESPACE