aboutsummaryrefslogtreecommitdiff
path: root/src/tests/optparser.cpp
blob: e26b175f5f5213adeafc3775651b186ba3e84d94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright (C) 2007-2023 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include <bu/optparser.h>
#include <bu/sio.h>
using namespace Bu;

class Opts : public Bu::OptParser
{
public:
    Opts() :
        iBob( 542 ),
        bVal( false )
    {
        addHelpBanner("optparser - Test some option things...");

        addHelpBanner("\nThis section represents options that actually have "
            "callbacks, or in the case of the new system, signals/slots.  They "
            "all take parameters, but if they return 0 then it will be as "
            "though they hadn't and the next thing will be processed normally.",
            true
            );
        addOption( slot( this, &Opts::yesparam ), 'x', "things",
            "This is the first test parameter.  It calls a function, and "
            "takes a parameter."
            );
        addOption( slot( this, &Opts::noparam ), 'y', "stuff",
            "This is the second test parameter.  It does not take "
            "parameters.  However, I do want to make this part much longer to "
            "see how it looks when you add way too much text to one of these "
            "things.  It can't really be that bad, right?"
            );

        addHelpBanner("\nThis section represents options with no callback or "
            "signal, but do have a variable to update.  They use the Formatter "
            "system and therefore it's very, very flexible.  Any data type "
            "you can read with a formatter you can set via parameter.",
            true
            );
        addOption( sVar, 's', "str", "Set a variable, see what it does.");
        addOption( bVal, 'b', "bool", "It's a thing.");
        addOption( iBob, "bob", "Change iBob to whatever you want.");
        addOption( dBob, 'd', "Change dBob to whatever you want.");

        setOverride("str", "Bob!");
        setHelpDefault("bob", "=542");
        setOverride("bool", true );
    
        addHelpOption();

        setNonOption( slot( this, &Opts::nonOption ) );
    }

    int yesparam( StrArray aParams )
    {
        sio << " - yesparam" << aParams << sio.nl;
        return 1;
    }

    int noparam( StrArray aParams )
    {
        sio << " - noparam" << aParams << sio.nl;
        return 0;
    }

    int nonOption( StrArray aParams )
    {
        sio << " - nonOption" << aParams << sio.nl;
        return aParams.getSize()-1;
    }

    int iBob;
    float dBob;
    Bu::String sVar;
    bool bVal;
};

int main( int argc, char *argv[] )
{
    Opts o;

    o.parse( argc, argv );

    sio << "sVar = \"" << o.sVar << "\"" << sio.nl;
    sio << "iBob = " << o.iBob << sio.nl;
    sio << "dBob = " << o.dBob << sio.nl;
    sio << "bVal = " << o.bVal << sio.nl;
}