examples/howto-parse-args.cxx: now with added comments and svn:keywords

git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7728 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
engelsman 2010-10-23 14:10:26 +00:00
parent a69425441a
commit 0cbfd64a28
1 changed files with 20 additions and 4 deletions

View File

@ -1,5 +1,5 @@
// //
// "$Id:$" // "$Id$"
// //
// How to parse command line arguments - Duncan Gibson 2010-10-23 // How to parse command line arguments - Duncan Gibson 2010-10-23
// First posted in http://www.fltk.org/newsgroups.php?gfltk.general+v:31449 // First posted in http://www.fltk.org/newsgroups.php?gfltk.general+v:31449
@ -7,6 +7,10 @@
// Shows how to decode additional command line arguments using Fl::args() // Shows how to decode additional command line arguments using Fl::args()
// on top of the "standard" options used by the toolkit itself. // on top of the "standard" options used by the toolkit itself.
// //
// Note that this only handles "option separateValue" rather than the
// usual *nix idiom of "option=value", and provides no validation nor
// conversion of the paramter string into ints or floats.
//
// Copyright 1998-2010 by Bill Spitzak and others. // Copyright 1998-2010 by Bill Spitzak and others.
// //
// This library is free software; you can redistribute it and/or // This library is free software; you can redistribute it and/or
@ -28,15 +32,25 @@
// //
// http://www.fltk.org/str.php // http://www.fltk.org/str.php
// //
#include <stdio.h>
#include <string.h>
#include <FL/Fl.H> #include <FL/Fl.H>
#include <FL/Fl_Box.H> #include <FL/Fl_Box.H>
#include <FL/Fl_Window.H> #include <FL/Fl_Window.H>
#include "stdio.h"
#include "string.h"
int helpFlag = 0; int helpFlag = 0;
char *optionString = 0; char *optionString = 0;
/*
* callback function passed to Fl::args() to parse individual argument.
* If there is a match, 'i' must be incremented by 2 or 1 as appropriate.
* If there is no match, Fl::args() will then call Fl::arg() as fallback
* to try to match the "standard" FLTK parameters.
*
* Returns 2 if argv[i] matches with required parameter in argv[i+1],
* returns 1 if argv[i] matches on its own,
* returns 0 if argv[i] does not match.
*/
int arg(int argc, char **argv, int &i) int arg(int argc, char **argv, int &i)
{ {
if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) { if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
@ -77,10 +91,12 @@ int main(int argc, char** argv)
Fl_Box* textBox = new Fl_Box(0, 0, 300, 200); Fl_Box* textBox = new Fl_Box(0, 0, 300, 200);
if (optionString != 0) if (optionString != 0)
textBox->label(optionString); textBox->label(optionString);
else
textBox->label("re-run with [-o|--option] text");
mainWin->show(argc, argv); mainWin->show(argc, argv);
return Fl::run(); return Fl::run();
} }
// //
// End of "$Id:$". // End of "$Id$".
// //