argumentparser.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #include <QDebug>
  2. #include <QRegExp>
  3. #include <QFile>
  4. #include <QStringList>
  5. #include <QFileInfo>
  6. #include <QUrl>
  7. #include "argumentparser.h"
  8. ArgumentParser::ArgumentParser(int argc, char *argv[])
  9. {
  10. // Accepted arguments
  11. m_settings = new QMap<QString, Settings::Parameter>;
  12. // Value types of accepted arguments
  13. m_parameterTypes = new QMap<Settings::Parameter, ParameterType>;
  14. m_settings->insert("--uid3", Settings::UID3);
  15. m_parameterTypes->insert(Settings::UID3, ArgumentParser::String);
  16. m_settings->insert("--fullscreen", Settings::ShowFullScreen);
  17. m_parameterTypes->insert(Settings::ShowFullScreen, ArgumentParser::Boolean);
  18. m_settings->insert("--softkeys", Settings::ShowSoftKeys);
  19. m_parameterTypes->insert(Settings::ShowSoftKeys, ArgumentParser::Boolean);
  20. m_settings->insert("--pannable", Settings::PanningEnabled);
  21. m_parameterTypes->insert(Settings::PanningEnabled, ArgumentParser::Boolean);
  22. m_settings->insert("--selection", Settings::TextSelectionEnabled);
  23. m_parameterTypes->insert(Settings::TextSelectionEnabled, ArgumentParser::Boolean);
  24. m_settings->insert("--log", Settings::LogFilePath);
  25. m_parameterTypes->insert(Settings::LogFilePath, ArgumentParser::ValidFile);
  26. m_settings->insert("--fullsdk", Settings::FullS60SDK);
  27. m_parameterTypes->insert(Settings::FullS60SDK, ArgumentParser::Boolean);
  28. m_settings->insert("--qtenv", Settings::QtEnvBatPath);
  29. m_parameterTypes->insert(Settings::QtEnvBatPath, ArgumentParser::Boolean);
  30. // Command options specifically for the CLI
  31. m_commandOptions = new QMap<QString, ParameterType>;
  32. m_commandOptions->insert("--mode", ArgumentParser::Option);
  33. m_commandOptions->insert("--gui", ArgumentParser::Boolean);
  34. m_commandOptions->insert("--platform", ArgumentParser::Option);
  35. // List of unrecognized arguments
  36. m_invalidArguments = new QStringList();
  37. // Default values
  38. m_type = Invalid;
  39. m_target = NO_TARGET;
  40. m_gui = true;
  41. m_platform = "symbian";
  42. // Parse the arguments
  43. parse(argc, argv);
  44. }
  45. ArgumentParser::~ArgumentParser()
  46. {
  47. delete m_settings;
  48. delete m_commandOptions;
  49. delete m_parameterTypes;
  50. delete m_invalidArguments;
  51. }
  52. /**
  53. * Tries to parse the command line arguments to the settings object.
  54. */
  55. void ArgumentParser::parse(int argc, char *argv[])
  56. {
  57. QTextStream cout(stdout, QIODevice::WriteOnly);
  58. for (int i = 1; i < argc; i++)
  59. {
  60. QRegExp rx;
  61. rx.setPattern("^(--[^=]+)=(.+)$");
  62. if (rx.indexIn(argv[i]) != -1)
  63. {
  64. // Check if the argument is one of recognized ones
  65. if (m_settings->contains(rx.cap(1)))
  66. {
  67. checkSetting(rx.cap(1), rx.cap(2));
  68. }
  69. else if (m_commandOptions->contains(rx.cap(1)))
  70. {
  71. checkOption(rx.cap(1), rx.cap(2));
  72. }
  73. else
  74. {
  75. m_invalidArguments->append(rx.cap(1));
  76. }
  77. }
  78. else
  79. {
  80. // This may be the project file, check that
  81. if (QFile::exists(argv[i]))
  82. {
  83. QFileInfo info(argv[i]);
  84. if (info.isDir())
  85. {
  86. // Generate from directory
  87. m_type = Directory;
  88. m_file = argv[i];
  89. }
  90. else if (info.isFile())
  91. {
  92. // Generate from widget
  93. m_type = Widget;
  94. m_file = argv[i];
  95. }
  96. }
  97. else
  98. {
  99. QUrl url = QUrl::fromUserInput(argv[i]);
  100. if (url.isValid())
  101. {
  102. // Generate from web
  103. m_type = URL;
  104. m_file = argv[i];
  105. }
  106. else
  107. {
  108. // Don't know what to do with this argument
  109. m_invalidArguments->append(argv[i]);
  110. }
  111. }
  112. }
  113. }
  114. foreach (QString value, *m_invalidArguments)
  115. {
  116. cout << "Ignoring invalid argument: " << value << "\n";
  117. }
  118. }
  119. /**
  120. * Checks if the given value for the setting is acceptable, and sets
  121. * a temporary value for Settings object if so.
  122. */
  123. bool ArgumentParser::checkSetting(QString setting, QString value)
  124. {
  125. // Check that the value is conformant
  126. Settings::Parameter key = m_settings->value(setting);
  127. switch (m_parameterTypes->value(key))
  128. {
  129. case ValidDirectory:
  130. if (QFile::exists(value))
  131. {
  132. Settings::setTemporary(key, value);
  133. return true;
  134. }
  135. break;
  136. case ValidFile:
  137. if (QFile::exists(value))
  138. {
  139. Settings::setTemporary(key, value);
  140. return true;
  141. }
  142. break;
  143. case Boolean:
  144. if (value == "true" || value == "false")
  145. {
  146. Settings::setTemporary(key, value == "true" ? true : false);
  147. return true;
  148. }
  149. break;
  150. case String:
  151. Settings::setTemporary(key, value);
  152. return true;
  153. case Option:
  154. default:
  155. break;
  156. }
  157. // If we haven't returned yet, the value was invalid and we'll return false
  158. m_invalidArguments->append(setting);
  159. return false;
  160. }
  161. /**
  162. * Checks if the given value for the CLI option is acceptable, and sets
  163. * a local variable that can be checked later.
  164. */
  165. bool ArgumentParser::checkOption(QString option, QString value)
  166. {
  167. // Simplistic check as we don't have that many options yet
  168. if (option == "--mode")
  169. {
  170. if (value == "preview")
  171. m_target = PREVIEW;
  172. else if (value == "build")
  173. m_target = BUILD;
  174. else if (value == "rebuild")
  175. m_target = REBUILD;
  176. else
  177. {
  178. m_invalidArguments->append(option);
  179. return false;
  180. }
  181. return true;
  182. }
  183. else if (option == "--gui")
  184. {
  185. m_gui = (value == "true" ? true : false);
  186. return true;
  187. }
  188. else if (option == "--platform")
  189. {
  190. if (value == "maemo" || value == "symbian")
  191. {
  192. m_platform = value;
  193. return true;
  194. }
  195. else
  196. {
  197. m_invalidArguments->append(option);
  198. return false;
  199. }
  200. }
  201. else
  202. {
  203. m_invalidArguments->append(option);
  204. return false;
  205. }
  206. }
  207. /**
  208. * Tells if we should have a silent, non-gui generator
  209. */
  210. bool ArgumentParser::guiEnabled() const
  211. {
  212. return m_gui;
  213. }
  214. /**
  215. * Returns the file that user wants to process.
  216. */
  217. QString ArgumentParser::getFile() const
  218. {
  219. return m_file;
  220. }
  221. /**
  222. * Returns the target platform
  223. */
  224. QString ArgumentParser::getPlatform() const
  225. {
  226. return m_platform;
  227. }
  228. /**
  229. * Returns the type of the given file.
  230. */
  231. ArgumentParser::FileType ArgumentParser::getFileType() const
  232. {
  233. return m_type;
  234. }
  235. /**
  236. * Returns the specified build action.
  237. */
  238. GeneratorTarget ArgumentParser::getTarget() const
  239. {
  240. return m_target;
  241. }