cprover
Toggle main menu visibility
Loading...
Searching...
No Matches
json_interface.cpp
Go to the documentation of this file.
1
/*******************************************************************\
2
3
Module: JSON Commandline Interface
4
5
Author: Peter Schrammel
6
7
\*******************************************************************/
8
11
12
#include "
json_interface.h
"
13
14
#include <
util/cmdline.h
>
15
#include <
util/exception_utils.h
>
16
#include <
util/json.h
>
17
#include <
util/message.h
>
18
19
#include "
json_parser.h
"
20
21
#include <iostream>
22
24
static
void
get_json_options
(
const
json_objectt
&
json
,
cmdlinet
&cmdline)
25
{
26
const
jsont
&arguments =
json
[
"arguments"
];
27
if
(!arguments.
is_array
())
28
{
29
throw
invalid_command_line_argument_exceptiont
(
30
"array expected"
,
"'arguments'"
);
31
}
32
33
for
(
const
auto
&argument :
to_json_array
(arguments))
34
{
35
if
(!argument.is_string())
36
{
37
throw
invalid_command_line_argument_exceptiont
(
38
"string expected"
,
"argument"
);
39
}
40
41
cmdline.
args
.push_back(argument.value);
42
}
43
44
const
jsont
&options =
json
[
"options"
];
45
if
(!options.
is_object
())
46
{
47
throw
invalid_command_line_argument_exceptiont
(
48
"array expected"
,
"'options'"
);
49
}
50
51
for
(
const
auto
&option_pair :
to_json_object
(options))
52
{
53
if
(option_pair.second.is_string() || option_pair.second.is_number())
54
{
55
// e.g. --option x
56
cmdline.
set
(option_pair.first, option_pair.second.value);
57
}
58
else
if
(option_pair.second.is_boolean())
59
{
60
// e.g. --flag
61
if
(option_pair.second.is_true())
62
cmdline.
set
(option_pair.first);
63
}
64
else
if
(option_pair.second.is_array())
65
{
66
// e.g. --option x --option y
67
for
(
const
auto
&element :
to_json_array
(option_pair.second))
68
{
69
if
(element.is_string())
70
cmdline.
set
(option_pair.first, element.value);
71
else
72
{
73
throw
invalid_command_line_argument_exceptiont
(
74
"string expected"
, option_pair.first);
75
}
76
}
77
}
78
else
79
{
80
throw
invalid_command_line_argument_exceptiont
(
81
"unrecognized commandline option format"
,
82
option_pair.first,
83
"Boolean, string, number, or string array expected"
);
84
}
85
}
86
}
87
88
void
json_interface
(
cmdlinet
&cmdline,
message_handlert
&message_handler)
89
{
90
if
(cmdline.
isset
(
"json-interface"
))
91
{
92
jsont
json
;
93
94
parse_json
(std::cin,
""
, message_handler,
json
);
95
96
try
97
{
98
if
(!
json
.is_object())
99
{
100
throw
invalid_command_line_argument_exceptiont
(
101
"JSON object expected at top-level"
,
"command-line JSON input"
);
102
}
103
104
get_json_options
(
to_json_object
(
json
), cmdline);
105
106
// Add this so that it gets propagated into optionst;
107
// the ui_message_handlert::uit has already been set on the basis
108
// of the json-interface flag.
109
cmdline.
set
(
"json-ui"
);
110
}
111
catch
(
const
invalid_command_line_argument_exceptiont
&e)
112
{
113
messaget
log(message_handler);
114
log.
error
() << e.
what
() <<
messaget::eom
;
115
116
// make sure we fail with a usage error
117
cmdline.
clear
();
118
}
119
}
120
}
cmdlinet
Definition
cmdline.h:20
cmdlinet::isset
virtual bool isset(char option) const
Definition
cmdline.cpp:30
cmdlinet::args
argst args
Definition
cmdline.h:154
cmdlinet::set
virtual void set(const std::string &option, bool value=true)
Set option option to value, or true if the value is omitted.
Definition
cmdline.cpp:63
cmdlinet::clear
virtual void clear()
Definition
cmdline.cpp:24
invalid_command_line_argument_exceptiont
Thrown when users pass incorrect command line arguments, for example passing no files to analysis or ...
Definition
exception_utils.h:51
invalid_command_line_argument_exceptiont::what
std::string what() const override
A human readable description of what went wrong.
Definition
exception_utils.cpp:17
json_objectt
Definition
json.h:298
jsont
Definition
json.h:27
jsont::is_array
bool is_array() const
Definition
json.h:61
jsont::is_object
bool is_object() const
Definition
json.h:56
message_handlert
Definition
message.h:27
messaget
Class that provides messages with a built-in verbosity 'level'.
Definition
message.h:154
messaget::error
mstreamt & error() const
Definition
message.h:391
messaget::eom
static eomt eom
Definition
message.h:289
cmdline.h
exception_utils.h
json.h
to_json_object
json_objectt & to_json_object(jsont &json)
Definition
json.h:442
to_json_array
json_arrayt & to_json_array(jsont &json)
Definition
json.h:424
get_json_options
static void get_json_options(const json_objectt &json, cmdlinet &cmdline)
Parse commandline options from json into cmdline.
Definition
json_interface.cpp:24
json_interface
void json_interface(cmdlinet &cmdline, message_handlert &message_handler)
Parses the JSON-formatted command line from stdin.
Definition
json_interface.cpp:88
json_interface.h
JSON Commandline Interface.
parse_json
bool parse_json(std::istream &in, const std::string &filename, message_handlert &message_handler, jsont &dest)
Definition
json_parser.cpp:27
json_parser.h
json
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition
properties.cpp:120
message.h
json
json_interface.cpp
Generated by
1.17.0