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 | args.push_back( argv[i] ); |
---|
60 | |
---|
61 | int i = 0; |
---|
62 | |
---|
63 | ArgTable::iterator it; |
---|
64 | bool finish; |
---|
65 | bool found; |
---|
66 | |
---|
67 | while ( i < args.size() ) |
---|
68 | { |
---|
69 | found = false; |
---|
70 | for ( it = argTable.begin(); it != argTable.end(); it++ ) |
---|
71 | { |
---|
72 | if ( matches( *it, args[i], finish ) ) |
---|
73 | { |
---|
74 | found = true; |
---|
75 | |
---|
76 | if ( it->numArgs + i >= args.size() ) |
---|
77 | { |
---|
78 | PRINTF(1)( "%s needs %d arguments!", args[i].c_str(), it->numArgs ); |
---|
79 | } |
---|
80 | |
---|
81 | std::vector<std::string> argArgs; |
---|
82 | |
---|
83 | for ( int j = 0; j < it->numArgs; j++ ) |
---|
84 | argArgs.push_back( args[i+j] ); |
---|
85 | |
---|
86 | if ( !cb( *it, data, args[i], argArgs ) ) |
---|
87 | return false; |
---|
88 | |
---|
89 | i += it->numArgs; |
---|
90 | |
---|
91 | if ( finish ) |
---|
92 | break; |
---|
93 | else |
---|
94 | { |
---|
95 | assert( it->numArgs == 0 ); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | if ( !found ) |
---|
101 | { |
---|
102 | PRINTF(1)("%s: illegal option\n", args[i].c_str()); |
---|
103 | return false; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | return true; |
---|
108 | } |
---|
109 | |
---|
110 | bool CmdLineParser::matches( ArgTableEntry entry, std::string arg, bool & finish ) |
---|
111 | { |
---|
112 | finish = true; |
---|
113 | |
---|
114 | if ( arg.length() < 2 ) |
---|
115 | return false; |
---|
116 | |
---|
117 | if ( arg[0] == '-' ) |
---|
118 | { |
---|
119 | if ( arg[1] == '-' ) |
---|
120 | { |
---|
121 | arg.erase( 0, 2 ); |
---|
122 | |
---|
123 | if ( arg.find('%') != std::string::npos ) |
---|
124 | { |
---|
125 | //TODO implement bether match algo |
---|
126 | assert( arg.find('%') == arg.length()-1 ); |
---|
127 | arg.erase( arg.length()-1, 1 ); |
---|
128 | return entry.longOption.find( arg ) == 0; |
---|
129 | } |
---|
130 | else |
---|
131 | { |
---|
132 | return entry.longOption == arg; |
---|
133 | } |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | if ( arg.length() != 2 && entry.numArgs != 0 ) |
---|
138 | { |
---|
139 | PRINTF(1)("using multiple flags together is only alowed if none needs an arugument. %s needs %d arguments\n", entry.shortOption, entry.numArgs); |
---|
140 | return false; |
---|
141 | } |
---|
142 | finish = false; |
---|
143 | return arg.find(entry.shortOption) != std::string::npos; |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|