You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.4KB

  1. //
  2. // "$Id: howto-parse-args.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $"
  3. //
  4. // How to parse command line arguments - Duncan Gibson 2010-10-23
  5. // First posted in http://www.fltk.org/newsgroups.php?gfltk.general+v:31449
  6. //
  7. // Shows how to decode additional command line arguments using Fl::args()
  8. // on top of the "standard" options used by the toolkit itself.
  9. //
  10. // Note that this only handles "option separateValue" rather than the
  11. // usual *nix idiom of "option=value", and provides no validation nor
  12. // conversion of the paramter string into ints or floats.
  13. //
  14. // Copyright 1998-2010 by Bill Spitzak and others.
  15. //
  16. // This library is free software; you can redistribute it and/or
  17. // modify it under the terms of the GNU Library General Public
  18. // License as published by the Free Software Foundation; either
  19. // version 2 of the License, or (at your option) any later version.
  20. //
  21. // This library is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. // Library General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU Library General Public
  27. // License along with this library; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  29. // USA.
  30. //
  31. // Please report all bugs and problems on the following page:
  32. //
  33. // http://www.fltk.org/str.php
  34. //
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <FL/Fl.H>
  38. #include <FL/Fl_Box.H>
  39. #include <FL/Fl_Window.H>
  40. int helpFlag = 0;
  41. char *optionString = 0;
  42. /*
  43. * callback function passed to Fl::args() to parse individual argument.
  44. * If there is a match, 'i' must be incremented by 2 or 1 as appropriate.
  45. * If there is no match, Fl::args() will then call Fl::arg() as fallback
  46. * to try to match the "standard" FLTK parameters.
  47. *
  48. * Returns 2 if argv[i] matches with required parameter in argv[i+1],
  49. * returns 1 if argv[i] matches on its own,
  50. * returns 0 if argv[i] does not match.
  51. */
  52. int arg(int argc, char **argv, int &i)
  53. {
  54. if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
  55. helpFlag = 1;
  56. i += 1;
  57. return 1;
  58. }
  59. if (strcmp("-o", argv[i]) == 0 || strcmp("--option", argv[i]) == 0) {
  60. if (i < argc-1 && argv[i+1] != 0) {
  61. optionString = argv[i+1];
  62. i += 2;
  63. return 2;
  64. }
  65. }
  66. return 0;
  67. }
  68. int main(int argc, char** argv)
  69. {
  70. int i = 1;
  71. if (Fl::args(argc, argv, i, arg) < argc)
  72. // note the concatenated strings to give a single format string!
  73. Fl::fatal("error: unknown option: %s\n"
  74. "usage: %s [options]\n"
  75. " -h | --help : print extended help message\n"
  76. " -o | --option # : example option with parameter\n"
  77. " plus standard fltk options\n",
  78. argv[i], argv[0]);
  79. if (helpFlag)
  80. Fl::fatal("usage: %s [options]\n"
  81. " -h | --help : print extended help message\n"
  82. " -o | --option # : example option with parameter\n"
  83. " plus standard fltk options:\n"
  84. "%s\n",
  85. argv[0], Fl::help);
  86. Fl_Window* mainWin = new Fl_Window(300, 200);
  87. Fl_Box* textBox = new Fl_Box(0, 0, 300, 200);
  88. if (optionString != 0)
  89. textBox->label(optionString);
  90. else
  91. textBox->label("re-run with [-o|--option] text");
  92. mainWin->show(argc, argv);
  93. return Fl::run();
  94. }
  95. //
  96. // End of "$Id: howto-parse-args.cxx 8183 2011-01-04 17:31:56Z AlbrechtS $".
  97. //