2010-10-23 17:19:37 +04:00
|
|
|
//
|
2020-08-20 13:42:53 +03:00
|
|
|
// How to parse command line arguments - Duncan Gibson 2010-10-23
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2020-08-20 13:42:53 +03:00
|
|
|
// First posted in https://www.fltk.org/newsgroups.php?gfltk.general+v:22493
|
|
|
|
// in thread "fltk-1.x : parsing command line arguments"
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2020-08-20 13:42:53 +03:00
|
|
|
// Shows how to decode additional command line arguments using Fl::args()
|
|
|
|
// on top of the "standard" options used by the toolkit itself.
|
2010-10-23 18:10:26 +04:00
|
|
|
//
|
2020-08-20 13:42:53 +03:00
|
|
|
// Note that this only handles "option separateValue" rather than the
|
|
|
|
// usual *nix idiom of "option=value", and provides no validation nor
|
|
|
|
// conversion of the parameter string into ints or floats.
|
|
|
|
//
|
2023-11-20 22:12:02 +03:00
|
|
|
// Example 1:
|
|
|
|
//
|
|
|
|
// ./howto-parse-args -ti "FLTK is great" -o "FLTK is a great GUI tool"
|
|
|
|
//
|
|
|
|
// Example 2: translated to Japanese and simplified Chinese, respectively,
|
|
|
|
// by a well known internet translation service.
|
|
|
|
//
|
|
|
|
// ./howto-parse-args -ti "FLTKは素晴らしいです" -o "FLTK 是一个很棒的 GUI 工具"
|
|
|
|
//
|
|
|
|
// Copyright 1998-2023 by Bill Spitzak and others.
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/COPYING.php
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// Please see the following page on how to report bugs and issues:
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2020-07-01 19:03:10 +03:00
|
|
|
// https://www.fltk.org/bugs.php
|
2010-10-23 17:19:37 +04:00
|
|
|
//
|
2010-10-23 18:10:26 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2010-10-23 17:19:37 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/Fl_Box.H>
|
|
|
|
#include <FL/Fl_Window.H>
|
|
|
|
|
|
|
|
int helpFlag = 0;
|
|
|
|
char *optionString = 0;
|
|
|
|
|
2010-10-23 18:10:26 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
2020-07-01 19:03:10 +03:00
|
|
|
*
|
2010-10-23 18:10:26 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2023-11-20 22:12:02 +03:00
|
|
|
int arg(int argc, char **argv, int &i) {
|
|
|
|
|
2010-10-23 17:19:37 +04:00
|
|
|
if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
|
|
|
|
helpFlag = 1;
|
|
|
|
i += 1;
|
|
|
|
return 1;
|
|
|
|
}
|
2023-11-20 22:12:02 +03:00
|
|
|
|
2010-10-23 17:19:37 +04:00
|
|
|
if (strcmp("-o", argv[i]) == 0 || strcmp("--option", argv[i]) == 0) {
|
|
|
|
if (i < argc-1 && argv[i+1] != 0) {
|
|
|
|
optionString = argv[i+1];
|
|
|
|
i += 2;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-20 22:12:02 +03:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
|
|
|
|
// Convert commandline arguments in 'argv' to UTF-8 on Windows.
|
|
|
|
// This is a no-op on all other platforms (see documentation).
|
|
|
|
|
|
|
|
Fl::args_to_utf8(argc, argv);
|
|
|
|
|
2010-10-23 17:19:37 +04:00
|
|
|
int i = 1;
|
|
|
|
if (Fl::args(argc, argv, i, arg) < argc)
|
|
|
|
// note the concatenated strings to give a single format string!
|
|
|
|
Fl::fatal("error: unknown option: %s\n"
|
|
|
|
"usage: %s [options]\n"
|
|
|
|
" -h | --help : print extended help message\n"
|
|
|
|
" -o | --option # : example option with parameter\n"
|
|
|
|
" plus standard fltk options\n",
|
|
|
|
argv[i], argv[0]);
|
|
|
|
if (helpFlag)
|
|
|
|
Fl::fatal("usage: %s [options]\n"
|
|
|
|
" -h | --help : print extended help message\n"
|
|
|
|
" -o | --option # : example option with parameter\n"
|
|
|
|
" plus standard fltk options:\n"
|
|
|
|
"%s\n",
|
|
|
|
argv[0], Fl::help);
|
|
|
|
|
|
|
|
Fl_Window* mainWin = new Fl_Window(300, 200);
|
|
|
|
Fl_Box* textBox = new Fl_Box(0, 0, 300, 200);
|
|
|
|
if (optionString != 0)
|
|
|
|
textBox->label(optionString);
|
2010-10-23 18:10:26 +04:00
|
|
|
else
|
|
|
|
textBox->label("re-run with [-o|--option] text");
|
2023-11-20 22:12:02 +03:00
|
|
|
|
|
|
|
mainWin->resizable(mainWin);
|
2010-10-23 17:19:37 +04:00
|
|
|
mainWin->show(argc, argv);
|
|
|
|
return Fl::run();
|
|
|
|
}
|