FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rstudio/src/cpp/core/ProgramOptions.cpp at master · pearsonca/rstudio · GitHub
pearsonca
/
rstudio
Public
forked from
rstudio/rstudio
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
ProgramOptions.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
244 lines (208 loc) · 7.47 KB
Breadcrumbs
rstudio
/
src
/
cpp
/
core
/
ProgramOptions.cpp
Copy path
File metadata and controls
244 lines (208 loc) · 7.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* ProgramOptions.cpp
*
* Copyright (C) 2009-19 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
#
include
<
core/ProgramOptions.hpp
>
#
include
<
iostream
>
#
include
<
shared_core/Error.hpp
>
#
include
<
core/Log.hpp
>
#
include
<
shared_core/FilePath.hpp
>
#
include
<
core/system/System.hpp
>
using
namespace
boost
::program_options
;
namespace
rstudio
{
namespace
core
{
namespace
program_options
{
namespace
{
bool
validateOptionsProvided
(
const
variables_map& vm,
const
options_description& optionsDescription,
const
std::string& configFile = std::string())
{
for
(
const
auto
& pOptionsDesc : optionsDescription.
options
())
{
std::string optionName = pOptionsDesc->
long_name
();
if
( !(vm.
count
(optionName)) )
{
std::string msg =
"
Required option
"
+ optionName +
"
not specified
"
;
if
(!configFile.
empty
())
msg +=
"
in config file
"
+ configFile;
reportError
(msg,
ERROR_LOCATION
);
return
false
;
}
}
//
all options validated
return
true
;
}
}
void
reportError
(
const
std::string& errorMessage,
const
ErrorLocation& location)
{
if
(
core::system::stderrIsTerminal
())
std::cerr << errorMessage << std::endl;
else
core::log::logErrorMessage
(errorMessage, location);
}
void
reportWarnings
(
const
std::string& warningMessages,
const
ErrorLocation& location)
{
if
(
core::system::stderrIsTerminal
())
std::cerr <<
"
WARNINGS:
"
<< warningMessages << std::endl;
else
core::log::logWarningMessage
(warningMessages, location);
}
void
parseCommandLine
(variables_map& vm,
const
OptionsDescription& optionsDescription,
const
options_description& commandLineOptions,
int
argc,
const
char
*
const
argv[],
std::vector<std::string>* pUnrecognized)
{
//
parse the command line
command_line_parser
parser
(argc,
const_cast
<
char
**>(argv));
parser.
options
(commandLineOptions);
parser.
positional
(optionsDescription.
positionalOptions
);
if
(pUnrecognized !=
nullptr
)
parser.
allow_unregistered
();
parsed_options parsed = parser.
run
();
store
(parsed, vm);
notify
(vm) ;
//
collect unrecognized if necessary
if
(pUnrecognized !=
nullptr
)
{
*pUnrecognized =
collect_unrecognized
(parsed.
options
,
include_positional);
}
}
bool
parseConfigFile
(variables_map& vm,
const
std::string& configFile,
const
OptionsDescription& optionsDescription,
bool
allowUnregisteredConfigOptions)
{
//
open the config file
if
(!configFile.
empty
())
{
std::shared_ptr<std::istream> pIfs;
Error error =
FilePath
(configFile).
openForRead
(pIfs);
if
(error)
{
reportError
(
"
Unable to open config file:
"
+ configFile,
ERROR_LOCATION
);
return
false
;
}
try
{
//
parse config file
store
(
parse_config_file
(*pIfs, optionsDescription.
configFile
, allowUnregisteredConfigOptions), vm) ;
notify
(vm) ;
}
catch
(
const
std::exception& e)
{
reportError
(
"
Error reading
"
+ configFile +
"
:
"
+
std::string
(e.
what
()),
ERROR_LOCATION
);
return
false
;
}
}
return
true
;
}
ProgramStatus
read
(
const
OptionsDescription& optionsDescription,
int
argc,
const
char
*
const
argv[],
std::vector<std::string>* pUnrecognized,
bool
* pHelp,
bool
allowUnregisteredConfigOptions,
bool
configFileHasPrecedence)
{
*pHelp =
false
;
std::string configFile;
try
{
//
general options
options_description
general
(
"
general
"
) ;
general.
add_options
()
(
"
help
"
,
"
print help message
"
)
(
"
test-config
"
,
"
test to ensure the config file is valid
"
)
(
"
config-file
"
,
value<std::string>(&configFile)->
default_value
(
optionsDescription.
defaultConfigFilePath
),
std::string
(
"
configuration file
"
).
c_str
());
//
make copy of command line options so we can add general to them
options_description
commandLineOptions
(optionsDescription.
commandLine
);
commandLineOptions.
add
(general);
variables_map vm ;
//
the order of parsing is determined based on whether or not the config file has precedence
//
if it does, parse it first, otherwise parse the command line first
if
(configFileHasPrecedence)
{
//
if we are parsing the config file first, do not attempt to parse
//
the config file path from the command line arguments - just use
//
the default value that was passed in
configFile = optionsDescription.
defaultConfigFilePath
;
if
(!
parseConfigFile
(vm, configFile, optionsDescription, allowUnregisteredConfigOptions))
return
ProgramStatus::exitFailure
();
parseCommandLine
(vm, optionsDescription, commandLineOptions, argc, argv, pUnrecognized);
}
else
{
parseCommandLine
(vm, optionsDescription, commandLineOptions, argc, argv, pUnrecognized);
//
"none" is a special sentinel value for the config-file which
//
explicitly prevents us from reading the default config file above
//
now that we are past that we can reset it to empty
if
(configFile ==
"
none
"
)
configFile =
"
"
;
if
(!
parseConfigFile
(vm, configFile, optionsDescription, allowUnregisteredConfigOptions))
return
ProgramStatus::exitFailure
();
}
//
show help if requested
if
(vm.
count
(
"
help
"
))
{
*pHelp =
true
;
std::cout << commandLineOptions ;
return
ProgramStatus::exitSuccess
() ;
}
//
validate all options are provided
else
{
if
(!
validateOptionsProvided
(vm, optionsDescription.
commandLine
))
return
ProgramStatus::exitFailure
();
if
(!configFile.
empty
())
{
if
(!
validateOptionsProvided
(vm,
optionsDescription.
configFile
,
configFile))
return
ProgramStatus::exitFailure
();
}
}
//
if this was a config-test then return exitSuccess, otherwise run
if
(vm.
count
(
"
test-config
"
))
{
return
ProgramStatus::exitSuccess
();
}
else
{
return
ProgramStatus::run
() ;
}
}
catch
(
const
boost::program_options::error& e)
{
std::string
msg
(e.
what
());
if
(!configFile.
empty
())
msg +=
"
in config file
"
+ configFile;
reportError
(msg,
ERROR_LOCATION
);
return
ProgramStatus::exitFailure
();
}
CATCH_UNEXPECTED_EXCEPTION
//
keep compiler happy
return
ProgramStatus::exitFailure
();
}
}
//
namespace program_options
}
//
namespace core
}
//
namespace rstudio
Back
|
FazBrowse Home
|
New Git URL