1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "cmdline_parser.h" |
---|
17 | |
---|
18 | using namespace std; |
---|
19 | |
---|
20 | |
---|
21 | /** |
---|
22 | * standard constructor |
---|
23 | */ |
---|
24 | CmdLineParser::CmdLineParser () |
---|
25 | { |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * standard deconstructor |
---|
31 | */ |
---|
32 | CmdLineParser::~CmdLineParser () |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, bool back ) |
---|
38 | { |
---|
39 | ArgTableEntry entry; |
---|
40 | |
---|
41 | entry.id = id; |
---|
42 | entry.longOption = longOption; |
---|
43 | entry.shortOption = shortOption; |
---|
44 | entry.numArgs = numArgs; |
---|
45 | |
---|
46 | if ( back ) |
---|
47 | argTable.push_back( entry ); |
---|
48 | else |
---|
49 | argTable.push_front( entry ); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | bool CmdLineParser::parse( ArgParserCallback cb, void * data, int argc, char ** argv ) |
---|
54 | { |
---|
55 | //put all args in vector |
---|
56 | std::vector<std::string> args; |
---|
57 | |
---|
58 | for ( int i = 1; i<argc; i++ ) |
---|
59 | { |
---|
60 | std::string s = argv[i]; |
---|
61 | |
---|
62 | if ( s.find( "=" ) == std::string::npos ) |
---|
63 | { |
---|
64 | args.push_back(s); |
---|
65 | } |
---|
66 | else |
---|
67 | { |
---|
68 | std::string op = s; |
---|
69 | std::string ar = s; |
---|
70 | op.erase( op.find("=") ); |
---|
71 | ar.erase( 0, ar.find("=")+1); |
---|
72 | |
---|
73 | PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str()); |
---|
74 | args.push_back( op ); |
---|
75 | args.push_back( ar ); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | int i = 0; |
---|
80 | |
---|
81 | ArgTable::iterator it; |
---|
82 | bool finish; |
---|
83 | bool found; |
---|
84 | |
---|
85 | while ( i < args.size() ) |
---|
86 | { |
---|
87 | found = false; |
---|
88 | for ( it = argTable.begin(); it != argTable.end(); it++ ) |
---|
89 | { |
---|
90 | if ( matches( *it, args[i], finish ) ) |
---|
91 | { |
---|
92 | found = true; |
---|
93 | |
---|
94 | if ( it->numArgs + i >= args.size() ) |
---|
95 | { |
---|
96 | PRINTF(1)( "%s needs %d arguments!\n", args[i].c_str(), it->numArgs ); |
---|
97 | return false; |
---|
98 | } |
---|
99 | |
---|
100 | std::vector<MultiType> argArgs; |
---|
101 | |
---|
102 | for ( int j = 1; j <= it->numArgs; j++ ) |
---|
103 | argArgs.push_back( args[i+j] ); |
---|
104 | |
---|
105 | if ( !cb( *it, data, args[i], argArgs ) ) |
---|
106 | return false; |
---|
107 | |
---|
108 | i += it->numArgs + 1; |
---|
109 | |
---|
110 | if ( finish ) |
---|
111 | break; |
---|
112 | else |
---|
113 | { |
---|
114 | assert( it->numArgs == 0 ); |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | if ( !found ) |
---|
120 | { |
---|
121 | PRINTF(1)("%s: illegal option\n", args[i].c_str()); |
---|
122 | return false; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return true; |
---|
127 | } |
---|
128 | |
---|
129 | bool CmdLineParser::matches( ArgTableEntry entry, std::string arg, bool & finish ) |
---|
130 | { |
---|
131 | finish = true; |
---|
132 | |
---|
133 | if ( arg.length() < 2 ) |
---|
134 | return false; |
---|
135 | |
---|
136 | if ( arg[0] == '-' ) |
---|
137 | { |
---|
138 | if ( arg[1] == '-' ) |
---|
139 | { |
---|
140 | arg.erase( 0, 2 ); |
---|
141 | |
---|
142 | if ( entry.longOption.find('%') != std::string::npos ) |
---|
143 | { |
---|
144 | //TODO implement bether match algo |
---|
145 | assert( entry.longOption.find('%') == entry.longOption.length()-1 ); |
---|
146 | std::string lo = entry.longOption; |
---|
147 | lo.erase( lo.length()-1, 1 ); |
---|
148 | PRINTF(0)("%s %s\n", arg.c_str(), lo.c_str()); |
---|
149 | return arg.find( lo ) == 0; |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | return arg.find( entry.longOption ) != std::string::npos; |
---|
154 | } |
---|
155 | } |
---|
156 | else |
---|
157 | { |
---|
158 | if ( arg.length() != 2 && entry.numArgs != 0 ) |
---|
159 | { |
---|
160 | PRINTF(1)("using multiple flags together is only alowed if none needs an arugument. %s needs %d arguments\n", entry.shortOption, entry.numArgs); |
---|
161 | return false; |
---|
162 | } |
---|
163 | finish = arg.length()==2; |
---|
164 | return arg.find(entry.shortOption) != std::string::npos; |
---|
165 | } |
---|
166 | } |
---|
167 | } |
---|