commonarguments.cc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. // vi:set ts=4 sts=4 sw=4 noet :
  3. //
  4. // Copyright 2010, 2011 wkhtmltopdf authors
  5. //
  6. // This file is part of wkhtmltopdf.
  7. //
  8. // wkhtmltopdf is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // wkhtmltopdf is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.
  20. #include "arghandler.inl"
  21. #include "commandlineparserbase.hh"
  22. #include <wkhtmltox/loadsettings.hh>
  23. using namespace wkhtmltopdf::settings;
  24. struct LoadErrorHandlingTM: public SomeSetterTM<LoadPage::LoadErrorHandling> {
  25. static LoadPage::LoadErrorHandling strToT(const char * val, bool &ok) {
  26. return strToLoadErrorHandling(val, &ok);
  27. }
  28. static QString TToStr(const LoadPage::LoadErrorHandling & o, bool & ok) {
  29. ok=true;
  30. return loadErrorHandlingToStr(o);
  31. }
  32. };
  33. typedef SomeSetter<LoadErrorHandlingTM> LoadErrorHandlingSetting;
  34. struct ProxyTM: public SomeSetterTM<Proxy> {
  35. static Proxy strToT(const char * val, bool &ok) {
  36. return strToProxy(val, &ok);
  37. }
  38. };
  39. /*!
  40. Argument handler setting a proxy variable
  41. */
  42. typedef SomeSetter<ProxyTM> ProxySetter;
  43. //All these function would have been lambda function, had C++ supported them, now we are forced to write them here
  44. /*!
  45. Lambda: Call the usage method
  46. */
  47. template <bool v>
  48. struct HelpFunc {
  49. bool operator()(const char **, CommandLineParserBase & p, char *) {
  50. p.usage(stdout,v);
  51. exit(0);
  52. }
  53. };
  54. /*!
  55. Lambda: Call the man method
  56. */
  57. struct ManPageFunc {
  58. bool operator()(const char **, CommandLineParserBase & p, char *) {
  59. p.manpage(stdout);
  60. exit(0);
  61. }
  62. };
  63. /*!
  64. Lambda: Call the man method
  65. */
  66. template <bool T>
  67. struct ReadmeFunc {
  68. bool operator()(const char **, CommandLineParserBase & p, char *) {
  69. p.readme(stdout, T);
  70. exit(0);
  71. }
  72. };
  73. /*!
  74. Lambda: Call the version method
  75. */
  76. struct VersionFunc {
  77. bool operator()(const char **, CommandLineParserBase & p, char *) {
  78. p.version(stdout);
  79. exit(0);
  80. }
  81. };
  82. /*!
  83. Lambda: show the license
  84. */
  85. struct LicenseFunc {
  86. bool operator()(const char **, CommandLineParserBase & p, char *) {
  87. p.license(stdout);
  88. exit(0);
  89. }
  90. };
  91. /*!
  92. The next arguments we add will belong to this section
  93. /param s The name of the section
  94. /param desc A description of the section
  95. */
  96. void CommandLineParserBase::section(QString s, QString desc) {
  97. currentSection = s;
  98. sectionDesc[s] = desc;
  99. sections.push_back(s);
  100. }
  101. /*!
  102. Indicate whether the next arguments we add require a patched qt to work
  103. /param h Do we require a patch
  104. */
  105. void CommandLineParserBase::qthack(bool h) {
  106. currentHack = h;
  107. }
  108. void CommandLineParserBase::mode(int m) {
  109. currentMode = m;
  110. }
  111. /*!
  112. Indicate whether the next arguments we add are "extended" and should not
  113. be shown in a simple --help
  114. \param e Are the arguments extended
  115. */
  116. void CommandLineParserBase::extended(bool e) {
  117. currentExtended = e;
  118. }
  119. /*!
  120. Add an argument to the list of arguments
  121. \param l The long "--" name of the argument
  122. \param s The short '-' name of the argument or 0 if unspecified
  123. \param d Description of the argument
  124. \param h The handler for the argument
  125. \param display Is the argument hidden
  126. */
  127. void CommandLineParserBase::addarg(QString l, char s, QString d, ArgHandler * h, bool display) {
  128. h->desc = d;
  129. h->longName = l;
  130. h->shortSwitch = s;
  131. h->display = display;
  132. h->qthack = currentHack;
  133. h->section = currentMode;
  134. h->extended = currentExtended;
  135. longToHandler[l] = h;
  136. if (s) shortToHandler[s] = h;
  137. sectionArgumentHandles[currentSection].push_back(h);
  138. }
  139. void CommandLineParserBase::addDocArgs() {
  140. extended(false);
  141. qthack(false);
  142. addarg("help", 'h', "Display help", new Caller<HelpFunc<false> >());
  143. addarg("version", 'V' ,"Output version information and exit", new Caller<VersionFunc>());
  144. addarg("license", 0 ,"Output license information and exit", new Caller<LicenseFunc>());
  145. addarg("extended-help", 'H',"Display more extensive help, detailing less common command switches", new Caller<HelpFunc<true> >());
  146. extended(true);
  147. qthack(false);
  148. addarg("manpage", 0, "Output program man page", new Caller<ManPageFunc>());
  149. addarg("htmldoc", 0, "Output program html help", new Caller<ReadmeFunc<true> >());
  150. addarg("readme", 0, "Output program readme", new Caller<ReadmeFunc<false> >());
  151. }
  152. void CommandLineParserBase::addGlobalLoadArgs(LoadGlobal & s) {
  153. extended(true);
  154. qthack(false);
  155. addarg("cookie-jar", 0, "Read and write cookies from and to the supplied cookie jar file", new QStrSetter(s.cookieJar, "path") );
  156. }
  157. void CommandLineParserBase::addWebArgs(Web & s) {
  158. extended(true);
  159. qthack(false);
  160. addarg("enable-plugins",0,"Enable installed plugins (plugins will likely not work)", new ConstSetter<bool>(s.enablePlugins,true));
  161. addarg("disable-plugins",0,"Disable installed plugins", new ConstSetter<bool>(s.enablePlugins,false));
  162. addarg("minimum-font-size",0,"Minimum font size", new IntSetter(s.minimumFontSize,"int"));
  163. addarg("user-style-sheet",0,"Specify a user style sheet, to load with every page", new QStrSetter(s.userStyleSheet,"url"));
  164. addarg("no-images",0,"Do not load or print images", new ConstSetter<bool>(s.loadImages, false));
  165. addarg("images",0,"Do load or print images", new ConstSetter<bool>(s.loadImages, true));
  166. addarg("disable-javascript",'n',"Do not allow web pages to run javascript", new ConstSetter<bool>(s.enableJavascript,false));
  167. addarg("enable-javascript",0,"Do allow web pages to run javascript", new ConstSetter<bool>(s.enableJavascript,true));
  168. extended(true);
  169. qthack(true);
  170. qthack(false);
  171. addarg("encoding", 0, "Set the default text encoding, for input", new QStrSetter(s.defaultEncoding,"encoding"));
  172. }
  173. void CommandLineParserBase::addPageLoadArgs(LoadPage & s) {
  174. extended(true);
  175. qthack(false);
  176. addarg("proxy",'p',"Use a proxy", new ProxySetter(s.proxy, "proxy"));
  177. addarg("username",0,"HTTP Authentication username", new QStrSetter(s.username, "username"));
  178. addarg("password",0,"HTTP Authentication password", new QStrSetter(s.password, "password"));
  179. addarg("load-error-handling", 0, "Specify how to handle pages that fail to load: abort, ignore or skip", new LoadErrorHandlingSetting(s.loadErrorHandling, "handler"));
  180. addarg("load-media-error-handling", 0, "Specify how to handle media files that fail to load: abort, ignore or skip", new LoadErrorHandlingSetting(s.mediaLoadErrorHandling, "handler"));
  181. addarg("custom-header",0,"Set an additional HTTP header (repeatable)", new MapSetter<>(s.customHeaders, "name", "value"));
  182. addarg("custom-header-propagation",0,"Add HTTP headers specified by --custom-header for each resource request.", new ConstSetter<bool>(s.repeatCustomHeaders, true));
  183. addarg("no-custom-header-propagation",0,"Do not add HTTP headers specified by --custom-header for each resource request.", new ConstSetter<bool>(s.repeatCustomHeaders, true));
  184. addarg("javascript-delay",0,"Wait some milliseconds for javascript finish", new IntSetter(s.jsdelay,"msec"));
  185. addarg("window-status",0,"Wait until window.status is equal to this string before rendering page", new QStrSetter(s.windowStatus, "windowStatus"));
  186. addarg("zoom",0,"Use this zoom factor", new FloatSetter(s.zoomFactor,"float",1.0));
  187. addarg("cookie",0,"Set an additional cookie (repeatable)", new MapSetter<>(s.cookies, "name", "value"));
  188. addarg("post", 0, "Add an additional post field (repeatable)", new MapSetter<PostItemCreator<false> >(s.post, "name", "value"));
  189. addarg("post-file", 0, "Post an additional file (repeatable)", new MapSetter<PostItemCreator<true> >(s.post, "name", "path"));
  190. addarg("disable-local-file-access", 0, "Do not allowed conversion of a local file to read in other local files, unless explicitly allowed with --allow", new ConstSetter<bool>(s.blockLocalFileAccess, true));
  191. addarg("enable-local-file-access", 0, "Allowed conversion of a local file to read in other local files.", new ConstSetter<bool>(s.blockLocalFileAccess, false));
  192. addarg("allow", 0, "Allow the file or files from the specified folder to be loaded (repeatable)", new StringListSetter(s.allowed,"path"));
  193. addarg("cache-dir", 0, "Web cache directory", new QStrSetter(s.cacheDir,"path"));
  194. addarg("debug-javascript", 0,"Show javascript debugging output", new ConstSetter<bool>(s.debugJavascript, true));
  195. addarg("no-debug-javascript", 0,"Do not show javascript debugging output", new ConstSetter<bool>(s.debugJavascript, false));
  196. addarg("stop-slow-scripts", 0, "Stop slow running javascripts", new ConstSetter<bool>(s.stopSlowScripts, true));
  197. addarg("no-stop-slow-scripts", 0, "Do not Stop slow running javascripts", new ConstSetter<bool>(s.stopSlowScripts, false));
  198. addarg("run-script", 0, "Run this additional javascript after the page is done loading (repeatable)", new StringListSetter(s.runScript, "js"));
  199. addarg("checkbox-svg", 0, "Use this SVG file when rendering unchecked checkboxes", new QStrSetter(s.checkboxSvg, "path", ""));
  200. addarg("checkbox-checked-svg", 0, "Use this SVG file when rendering checked checkboxes", new QStrSetter(s.checkboxCheckedSvg, "path" ,""));
  201. addarg("radiobutton-svg", 0, "Use this SVG file when rendering unchecked radiobuttons", new QStrSetter(s.radiobuttonSvg, "path", ""));
  202. addarg("radiobutton-checked-svg", 0, "Use this SVG file when rendering checked radiobuttons", new QStrSetter(s.radiobuttonCheckedSvg, "path", ""));
  203. }