home *** CD-ROM | disk | FTP | other *** search
- //: C20:ProgValTest.cpp
- // From Thinking in C++, 2nd Edition
- // Available at http://www.BruceEckel.com
- // (c) Bruce Eckel 1999
- // Copyright notice in Copyright.txt
- //{L} ProgVals
- #include "ProgVals.h"
- using namespace std;
-
- string defaults[][2] = {
- { "color", "red" },
- { "size", "medium" },
- { "shape", "rectangular" },
- { "action", "hopping"},
- };
-
- const char* usage = "usage:\n"
- "ProgValTest [flag1=val1 flag2=val2 ...]\n"
- "(Note no space around '=')\n"
- "Where the flags can be any of: \n"
- "color, size, shape, action \n";
-
- // So it can be used globally:
- ProgVals pvals(defaults,
- sizeof defaults / sizeof *defaults);
-
- class Animal {
- string color, size, shape, action;
- public:
- Animal(string col, string sz,
- string shp, string act)
- :color(col),size(sz),shape(shp),action(act){}
- // Default constructor uses program default
- // values, possibly change on command line:
- Animal() : color(pvals["color"]),
- size(pvals["size"]), shape(pvals["shape"]),
- action(pvals["action"]) {}
- void print() {
- cout << "color = " << color << endl
- << "size = " << size << endl
- << "shape = " << shape << endl
- << "action = " << action << endl;
- }
- // And of course pvals can be used anywhere
- // else you'd like.
- };
-
- int main(int argc, char* argv[]) {
- // Initialize and parse command line values
- // before any code that uses pvals is called:
- pvals.parse(argc, argv, usage);
- pvals.print();
- Animal a;
- cout << "Animal a values:" << endl;
- a.print();
- } ///:~
-