1 /*********************************
2 ** Tsunagari Tile Engine **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
12 CommandLineOptions::CommandLineOptions(int argc
, char** argv
) :
13 argc(argc
), argv(argv
)
17 CommandLineOptions::~CommandLineOptions()
21 for (opt
= 0; opt
< OptionsList
.size(); opt
++)
22 delete OptionsList
[opt
];
25 void CommandLineOptions::insert(std::string shortopt
, std::string longopt
,
26 std::string argument
, std::string description
)
28 Option
* opt
= new Option
;
29 opt
->shortopt
= shortopt
;
30 opt
->longopt
= longopt
;
31 opt
->argument
= argument
;
32 opt
->description
= description
;
35 OptionsList
.push_back(opt
);
38 bool CommandLineOptions::parse()
44 for (arg
= 1; arg
< argc
; arg
++) {
46 for (opt
= 0; opt
< OptionsList
.size(); opt
++) {
47 if (!OptionsList
[opt
]->shortopt
.compare(argv
[arg
]) ||
48 !OptionsList
[opt
]->longopt
.compare(argv
[arg
])) {
49 found
= true; /* We found the option. */
50 OptionsList
[opt
]->present
= true;
51 if (!OptionsList
[opt
]->argument
.empty()) {
53 OptionsList
[opt
]->value
=
63 if (!found
) /* We didn't find the option. */
69 bool CommandLineOptions::check(std::string longopt
)
73 for (search
= 0; search
< OptionsList
.size(); search
++) {
74 if (!OptionsList
[search
]->longopt
.compare(longopt
))
75 return OptionsList
[search
]->present
;
81 std::string
CommandLineOptions::get(std::string longopt
)
85 for (search
= 0; search
< OptionsList
.size(); search
++) {
86 if (!OptionsList
[search
]->longopt
.compare(longopt
))
87 return OptionsList
[search
]->value
;
93 void CommandLineOptions::usage()
98 usageSize(&optmaxlen
, &argmaxlen
);
100 usagePrintLong(optmaxlen
, argmaxlen
);
103 void CommandLineOptions::usageSize(int* optmaxlen
, int* argmaxlen
)
107 for (opt
= 0; opt
< OptionsList
.size(); opt
++) {
108 if (OptionsList
[opt
]->shortopt
.size() +
109 OptionsList
[opt
]->longopt
.size() > (unsigned)*optmaxlen
)
110 *optmaxlen
= (int)OptionsList
[opt
]->shortopt
.size() +
111 (int)OptionsList
[opt
]->longopt
.size() + 1;
112 if ((int)OptionsList
[opt
]->argument
.size() > *argmaxlen
)
113 *argmaxlen
= (int)OptionsList
[opt
]->argument
.size();
117 void CommandLineOptions::usagePrintShort()
119 fprintf(stderr
, "Usage: %s [OPTIONS]", argv
[0]);
122 void CommandLineOptions::usagePrintLong(int optmaxlen
, int argmaxlen
)
126 fprintf(stderr
, "\n\nAvailable options:\n");
128 for (opt
= 0; opt
< OptionsList
.size(); opt
++) {
129 if (!OptionsList
[opt
]->shortopt
.empty())
130 fprintf(stderr
, " %s,",
131 OptionsList
[opt
]->shortopt
.c_str());
133 fprintf(stderr
, " ");
135 (std::string("%-")+itostr(optmaxlen
+1)+"s").c_str(),
136 OptionsList
[opt
]->longopt
.c_str());
137 if (!OptionsList
[opt
]->argument
.empty())
139 (std::string("%-")+itostr(argmaxlen
+3)+"s").c_str(),
140 OptionsList
[opt
]->argument
.c_str());
143 (std::string("%-")+itostr(argmaxlen
+3)+"c").c_str(),
145 fprintf(stderr
, "%s\n",
146 OptionsList
[opt
]->description
.c_str());
149 fprintf(stderr
, "\n\n");