18 #include "boost/program_options.hpp" 19 #include "cetlib/container_algorithms.h" 20 #include "cetlib_except/exception.h" 21 #include "cetlib/filepath_maker.h" 22 #include "cetlib/includer.h" 33 std::string
const fhicl_env_var{
"FHICL_FILE_PATH"};
36 std::string
const help{
"Help"};
37 std::string
const processing{
"Processing"};
40 using strings = std::vector<string>;
43 string error_filename;
44 string output_filename;
45 strings input_filenames;
50 Options process_arguments(
int argc,
char* argv[]);
52 std::unique_ptr<cet::filepath_maker> get_policy(
53 int const lookup_policy,
54 std::string
const& lookup_path);
56 int do_including(std::string
const& starting_filename,
57 cet::filepath_maker& lookup_policy,
65 main(
int argc,
char* argv[])
70 opts = process_arguments(argc, argv);
73 if (e.category() == help)
75 if (e.category() == processing)
80 std::ofstream outfile(opts.output_filename.c_str());
81 std::ofstream errfile(opts.error_filename.c_str());
83 std::ostream& out = opts.output_filename.empty() ? std::cout : outfile;
84 std::ostream& err = opts.error_filename.empty() ? std::cerr : errfile;
86 auto const policy = get_policy(opts.lookup_policy, opts.lookup_path);
89 for (std::string
const& filename : opts.input_filenames)
90 nfailures += do_including(filename, *policy, out, err);
100 process_arguments(
int argc,
char* argv[])
103 namespace bpo = boost::program_options;
107 bpo::options_description desc(
"fhicl-expand <options> [files]\nOptions");
110 desc.add_options()(
"help,h",
"produce help message")(
111 "inputs,i", bpo::value<strings>(&opts.input_filenames),
"input files")(
112 "output,o", bpo::value<string>(&opts.output_filename),
"output file")(
113 "error,e", bpo::value<string>(&opts.error_filename),
"error file")(
115 bpo::value<int>(&opts.lookup_policy)->default_value(1),
116 "lookup policy code:" 117 "\n 0 => cet::filepath_maker" 118 "\n 1 => cet::filepath_lookup" 119 "\n 2 => cet::filepath_lookup_nonabsolute" 120 "\n 3 => cet::filepath_lookup_after1")(
122 bpo::value<string>(&opts.lookup_path)->default_value(fhicl_env_var),
123 "path or environment variable to be used by lookup-policy");
125 bpo::positional_options_description pd;
126 pd.add(
"inputs", -1);
128 bpo::variables_map varmap;
131 bpo::command_line_parser(argc, argv).options(desc).positional(pd).run(),
136 std::ostringstream err_stream;
137 err_stream <<
"Error processing command line in " << argv[0] <<
": " 138 << err.what() <<
'\n';
144 if (varmap.count(
"help")) {
145 std::cout << desc <<
"\n";
149 if (opts.input_filenames.empty())
150 opts.input_filenames.push_back(
"-");
155 std::unique_ptr<cet::filepath_maker>
156 get_policy(
int const lookup_policy, std::string
const& lookup_path)
158 std::cerr <<
"Policy is " << lookup_policy <<
"; path is \"" << lookup_path
160 switch (lookup_policy) {
162 return std::make_unique<cet::filepath_maker>();
164 return std::make_unique<cet::filepath_lookup>(lookup_path);
166 return std::make_unique<cet::filepath_lookup_nonabsolute>(lookup_path);
168 return std::make_unique<cet::filepath_lookup_after1>(lookup_path);
170 std::ostringstream err_stream;
171 err_stream <<
"Error: command line lookup-policy " << lookup_policy
172 <<
" is unknown; choose 0, 1, 2, or 3\n";
173 throw std::runtime_error(err_stream.str());
178 do_including(std::string
const& starting_filename,
179 cet::filepath_maker& lookup_policy,
181 std::ostream& err)
try {
182 cet::includer inc{starting_filename, lookup_policy};
183 cet::copy_all(inc, std::ostream_iterator<char>{to});
187 err <<
"Error for file: " << starting_filename <<
'\n' << e.what() <<
'\n';
int main(int argc, char *argv[])
cet::coded_exception< error, detail::translate > exception