> Be aware that if you need trailing [non-option] arguments, such as
> filenames, that you need to put an explicit '--' in the command line.

  AND you also have to modify the code in howto-parse-args.cxx too.

I append a more complete version that handles decoding options, '--'
and a trailing filename. It allows:

./flApp -b 1000 -1 10 -2 20 -- dataFile.txt

You will need to add a loop to handle multiple trailing filenames, and
Yes! I know that 1 and 2 are not the best choice for short opt names :-P

Cheers
D.

// this won't compile because there's lots of stuff omitted

/*
 * global flags and option strings!
 */
int helpFlag = 0;
int debugFlag = 0;
const char* optionNodes1 = 0;
const char* optionNodes2 = 0;
const char* optionNbase  = 0;
const char* optionMinMax = 0;

int arg(int argc, char **argv, int &i)
{
    if (strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
        helpFlag = 1;
        i += 1;
        return 1;
    }
    if (strcmp("-d", argv[i]) == 0 || strcmp("--debug", argv[i]) == 0) {
        debugFlag = 1;
        i += 1;
        return 1;
    }
    if (strcmp("-1", argv[i]) == 0 || strcmp("--nodes1", argv[i]) == 0) {
        if (i < argc-1 && argv[i+1] != 0) {
            optionNodes1 = argv[i+1];
            i += 2;
            return 2;
        }
    }
    if (strcmp("-2", argv[i]) == 0 || strcmp("--nodes2", argv[i]) == 0) {
        if (i < argc-1 && argv[i+1] != 0) {
            optionNodes2 = argv[i+1];
            i += 2;
            return 2;
        }
    }
    if (strcmp("-b", argv[i]) == 0 || strcmp("--nbase", argv[i]) == 0) {
        if (i < argc-1 && argv[i+1] != 0) {
            optionNbase = argv[i+1];
            i += 2;
            return 2;
        }
    }
    if (strcmp("-m", argv[i]) == 0 || strcmp("--minmax", argv[i]) == 0) {
        if (i < argc-1 && argv[i+1] != 0) {
            optionMinMax = argv[i+1];
            i += 2;
            return 2;
        }
    }
    return 0;
}

int extractInt(const char* text)
{
    int n, bytes = 0, value = 0;
    n = sscanf(text, "%d%n", &value, &bytes);
    assert(n == 1 && bytes == strlen(text));
    return value;
}

double extractDbl(const char* text)
{
    int n, bytes = 0;
    double value = 0.0;
    n = sscanf(text, "%lf%n", &value, &bytes);
    assert(n == 1 && bytes == strlen(text));
    return value;
}

int main(int argc, char* argv[])
{
    int i = 1;
    if (Fl::args(argc, argv, i, arg) < argc && strcmp("--", argv[i]) != 0)
        Fl::fatal(  "error: unknown option: %s\n"
                    "usage: %s [options] -- fileName.dat\n"
                    " -h | --help     : print extended help message\n"
                    " -1 | --nodes1 # : horizontal elements [default=%d]\n"
                    " -2 | --nodes2 # : vertical elements [default=%d]\n"
                    " -b | --nbase  # : base node number [default=%d]\n"
                    " plus standard fltk options\n",
                    argv[i], argv[0],
                    Box::def_nodes1, Box::def_nodes2, Box::def_nbase);
    if (helpFlag)
        Fl::fatal(  "usage: %s [options] -- fileName.dat\n"
                    " -h | --help     : print extended help message\n"
                    " -1 | --nodes1 # : horizontal elements [default=%d]\n"
                    " -2 | --nodes2 # : vertical elements [default=%d]\n"
                    " -b | --nbase  # : base node number [default=%d]\n"
                    " plus standard fltk options:\n"
                    "%s\n",
                    argv[0],
                    Box::def_nodes1, Box::def_nodes2, Box::def_nbase,
                    Fl::help);

    Fl_Window* w = new Fl_Window(400, 430);
    Box* b = new Box(5, 5, 390, 390);
    LightButtons* lbs = new LightButtons(0, 400, 400, 30);
    lbs->bx = b;
    w->end();

    b->debug = debugFlag;

    if (optionNodes1 != 0)
        b->nodes1 = extractInt(optionNodes1);
    if (optionNodes2 != 0)
        b->nodes2 = extractInt(optionNodes2);
    if (optionNbase != 0)
        b->nbase = extractInt(optionNbase);

    if (optionMinMax != 0) {
        MinMaxReader mmr;
        mmr.readFile(optionMinMax);
    }

    if (argc > 1)
        b->readFile(argv[argc-1]);

    w->show(argc, argv);
    Fl::run();
    return(0);
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to