Debian Codegraph @

utils/fgviewer/ArgumentParser.cxx

raw symbols SCIP
1 // ArgumentParser.cxx -- flightgear viewer argument parser
2 //
3 // Copyright (C) 2021 by Erik Hofman
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19
20 #include <cstring>
21 #include <iostream>
22 #include <string>
23 #include <map>
24
25 #include "ArgumentParser.hxx"
26
27 ArgumentParser::ArgumentParser(int argc, char **argv) :
28 arg_num(argc),
29 arg_values(argv),
30 appName(argv[0]),
31 arguments( osg::ArgumentParser(&arg_num, arg_values) )
32 {
33 for (int i=1; i<argc; i++)
34 {
35 std::string arg = argv[i];
36 std::string val;
37
38 if (arg[0] == '-')
39 {
40 std::size_t pos = arg.find('=');
41 if (pos != std::string::npos)
42 {
43 val = arg.substr(pos+1);
44 arg = arg.substr(0, pos);
45 }
46 else if ((i+1)<argc && argv[i+1][0] != '-') {
47 val = argv[++i];
48 } else {
49 val = "";
50 }
51
52 args[arg] = val;
53 }
54 else {
55 files.push_back(arg);
56 }
57 }
58 }
59
60 bool
61 ArgumentParser::read(const char *arg)
62 {
63 auto it = args.find(arg);
64 if (it != args.end())
65 {
66 args.erase(it);
67 return true;
68 }
69 return false;
70 }
71
72 bool
73 ArgumentParser::read(const char *arg, std::string& value)
74 {
75 auto it = args.find(arg);
76 if (it != args.end())
77 {
78 value = it->second;
79 args.erase(it);
80 return true;
81 }
82 return false;
83 }
84
85 bool
86 ArgumentParser::read(const char *arg, std::string& name, std::string& value)
87 {
88 auto it = args.find(arg);
89
90 // old fgviewer behavior: --prop name=value
91 if (it != args.end()) {
92 std::string str = it->second;
93
94 std::size_t pos = str.find("=");
95 if (pos != std::string::npos && pos != str.size())
96 {
97 value = str.substr(pos);
98 name = str.substr(0, pos);
99 args.erase(it);
100 return true;
101 }
102 }
103
104 // FlightGear behavior: --prop:name=value
105 std::string str = name;
106 str += ':';
107 str += name;
108
109 it = args.find(str);
110 if (it != args.end())
111 {
112 value = it->second;
113 args.erase(it);
114 return true;
115 }
116
117 return false;
118 }
119
120 void
121 ArgumentParser::reportRemainingOptionsAsUnrecognized()
122 {
123 for (auto it : args) {
124 std::cerr << "Unsupported argument: " << it.first << std::endl;
125 }
126 }
127
128
129 void
130 ArgumentParser::writeErrorMessages(std::ostream& output)
131 {
132 }