54 std::pair<targetIterator, bool> res =
m_targets.insert( { tgt.
name, tgt } );
55 res.first->second.orderAdded =
nAdded;
62 const std::string &so,
63 const std::string &lo,
66 const std::string &kw,
68 const std::string &ht,
69 const std::string &he )
85 if( it->second.shortOpt ==
"" && it->second.longOpt ==
"" )
88 clOpts.
add( it->second.name, it->second.shortOpt.c_str(), it->second.longOpt.c_str(), it->second.clType );
95 if( cloit->shortOpt ==
"" && cloit->longOpt ==
"" )
98 clOpts.
add( cloit->name, cloit->shortOpt.c_str(), cloit->longOpt.c_str(), cloit->clType );
106 std::vector<std::string> unk;
109 for(
size_t n = 0; n < unk.size(); ++n )
120 if( clOpts.
nOpts == 0 )
127 if( oneTarget !=
"" && it->second.name != oneTarget )
130 if( clOpts.
optSet( it->second.name ) )
132 std::vector<std::string> args;
134 clOpts.
getAll( args, it->second.name );
136 it->second.values.insert( it->second.values.end(), args.begin(), args.end() );
139 for(
size_t n = 0; n < args.size(); ++n )
140 it->second.sources.push_back(
"command line" );
143 it->second.verbosity = clOpts.
count( it->second.name );
144 it->second.set =
true;
162 if( !reportFileNotFound )
178 "Parsing error in " + fname +
" at line " + std::to_string( prv ) );
186 if( iF.
count( it->second.section, it->second.keyword ) > 0 )
188 it->second.values.push_back( iF( it->second.section, it->second.keyword ) );
191 it->second.sources.push_back( fname );
193 it->second.set =
true;
195 iF.
erase( it->second.section, it->second.keyword );
200 for(
auto iFit = iF.
names.begin(); iFit != iF.
names.end(); ++iFit )
205 std::string sect, nam;
223 if( targets.count( name ) == 0 )
226 targets[name].used =
true;
228 return targets[name].set;
238 targets[name].used =
true;
239 return targets[name].values.size();
249 targets[name].used =
true;
250 return targets[name].verbosity;
263 const std::string &name,
265 std::unordered_map<std::string, configTarget> &targets );
268 const std::string &name,
270 std::unordered_map<std::string, configTarget> &targets );
273 const std::string &name,
275 std::unordered_map<std::string, configTarget> &targets );
278 const std::string &name,
280 std::unordered_map<std::string, configTarget> &targets );
283 const std::string &name,
285 std::unordered_map<std::string, configTarget> &targets );
288 const std::string &name,
290 std::unordered_map<std::string, configTarget> &targets );
293 const std::string &name,
295 std::unordered_map<std::string, configTarget> &targets );
298 const std::string &name,
300 std::unordered_map<std::string, configTarget> &targets );
303 const std::string &name,
305 std::unordered_map<std::string, configTarget> &targets );
308 const std::string &name,
310 std::unordered_map<std::string, configTarget> &targets );
313 const std::string &name,
315 std::unordered_map<std::string, configTarget> &targets );
318 const std::string &name,
320 std::unordered_map<std::string, configTarget> &targets );
323 const std::string &name,
325 std::unordered_map<std::string, configTarget> &targets );
328 const std::string &name,
330 std::unordered_map<std::string, configTarget> &targets );
334 const std::string &name,
336 std::unordered_map<std::string, configTarget> &targets );
340 const std::string &name,
342 std::unordered_map<std::string, configTarget> &targets );
345 const std::string &name,
347 std::unordered_map<std::string, configTarget> &targets );
393 const std::string &name,
394 std::unordered_map<std::string, configTarget> &targets );
397 const std::string &name,
398 std::unordered_map<std::string, configTarget> &targets );
407 const std::string &name,
408 std::unordered_map<std::string, configTarget> &targets );
411 const std::string &name,
412 std::unordered_map<std::string, configTarget> &targets );
415 const std::string &name,
416 std::unordered_map<std::string, configTarget> &targets );
419 const std::string &name,
420 std::unordered_map<std::string, configTarget> &targets );
423 const std::string &name,
424 std::unordered_map<std::string, configTarget> &targets );
427 const std::string &name,
428 std::unordered_map<std::string, configTarget> &targets );
431 const std::string &name,
432 std::unordered_map<std::string, configTarget> &targets );
435 const std::string &name,
436 std::unordered_map<std::string, configTarget> &targets );
439 const std::string &name,
440 std::unordered_map<std::string, configTarget> &targets );
444 const std::string &name,
445 std::unordered_map<std::string, configTarget> &targets );
452 const std::string &name,
453 std::unordered_map<std::string, configTarget> &targets );
496 const std::string &name,
498 std::unordered_map<std::string, configTarget> &targets );
501 const std::string &name,
503 std::unordered_map<std::string, configTarget> &targets );
506 const std::string &name,
508 std::unordered_map<std::string, configTarget> &targets );
511 const std::string &name,
513 std::unordered_map<std::string, configTarget> &targets );
516 const std::string &name,
518 std::unordered_map<std::string, configTarget> &targets );
521 const std::string &name,
523 std::unordered_map<std::string, configTarget> &targets );
526 const std::string &name,
528 std::unordered_map<std::string, configTarget> &targets );
531 const std::string &name,
533 std::unordered_map<std::string, configTarget> &targets );
536 const std::string &name,
538 std::unordered_map<std::string, configTarget> &targets );
541 const std::string &name,
543 std::unordered_map<std::string, configTarget> &targets );
546 const std::string &name,
548 std::unordered_map<std::string, configTarget> &targets );
551 const std::string &name,
553 std::unordered_map<std::string, configTarget> &targets );
556 const std::string &name,
558 std::unordered_map<std::string, configTarget> &targets );
561 const std::string &name,
563 std::unordered_map<std::string, configTarget> &targets );
567 const std::string &name,
569 std::unordered_map<std::string, configTarget> &targets );
573 const std::string &name,
575 std::unordered_map<std::string, configTarget> &targets );
578 const std::string &name,
580 std::unordered_map<std::string, configTarget> &targets );
623 const std::string &name,
624 std::unordered_map<std::string, configTarget> &targets );
627 const std::string &name,
628 std::unordered_map<std::string, configTarget> &targets );
631 const std::string &name,
632 std::unordered_map<std::string, configTarget> &targets );
635 const std::string &name,
636 std::unordered_map<std::string, configTarget> &targets );
639 const std::string &name,
640 std::unordered_map<std::string, configTarget> &targets );
643 const std::string &name,
644 std::unordered_map<std::string, configTarget> &targets );
647 const std::string &name,
648 std::unordered_map<std::string, configTarget> &targets );
651 const std::string &name,
652 std::unordered_map<std::string, configTarget> &targets );
655 const std::string &name,
656 std::unordered_map<std::string, configTarget> &targets );
659 const std::string &name,
660 std::unordered_map<std::string, configTarget> &targets );
663 const std::string &name,
664 std::unordered_map<std::string, configTarget> &targets );
667 const std::string &name,
668 std::unordered_map<std::string, configTarget> &targets );
671 const std::string &name,
672 std::unordered_map<std::string, configTarget> &targets );
675 const std::string &name,
676 std::unordered_map<std::string, configTarget> &targets );
680 const std::string &name,
681 std::unordered_map<std::string, configTarget> &targets );
685 const std::string &name,
686 std::unordered_map<std::string, configTarget> &targets );
689 const std::string &name,
690 std::unordered_map<std::string, configTarget> &targets );
731template int appConfigurator::operator()<
char>(
char &v,
const std::string &name );
733template int appConfigurator::operator()<
signed char>(
signed char &v,
const std::string &name );
735template int appConfigurator::operator()<
short>(
short &v,
const std::string &name );
737template int appConfigurator::operator()<
int>(
int &v,
const std::string &name );
739template int appConfigurator::operator()<
long>(
long &v,
const std::string &name );
741template int appConfigurator::operator()<
long long>(
long long &v,
const std::string &name );
743template int appConfigurator::operator()<
unsigned char>(
unsigned char &v,
const std::string &name );
745template int appConfigurator::operator()<
unsigned short>(
unsigned short &v,
const std::string &name );
747template int appConfigurator::operator()<
unsigned int>(
unsigned int &v,
const std::string &name );
749template int appConfigurator::operator()<
unsigned long>(
unsigned long &v,
const std::string &name );
751template int appConfigurator::operator()<
unsigned long long>(
unsigned long long &v,
const std::string &name );
753template int appConfigurator::operator()<
float>(
float &v,
const std::string &name );
755template int appConfigurator::operator()<
double>(
double &v,
const std::string &name );
757template int appConfigurator::operator()<
long double>(
long double &v,
const std::string &name );
760template int appConfigurator::operator()<__float128>( __float128 &v,
const std::string &name );
763template int appConfigurator::operator()<
bool>(
bool &v,
const std::string &name );
765template int appConfigurator::operator()<std::string>( std::string &v,
const std::string &name );
822 const std::string §ion,
823 const std::string &keyword );
826 const std::string §ion,
827 const std::string &keyword );
833 const std::string §ion,
834 const std::string &keyword );
837 const std::string §ion,
838 const std::string &keyword );
866 std::string sect = it->second.section;
871 for(
size_t i = 0; i < sections.size(); ++i )
873 if( sections[i] == sect )
882 sections.push_back( sect );
894 const std::vector<std::string> §ions,
895 const std::vector<std::string> &keywords,
896 const std::vector<std::string> &values )
902 std::string lastSection;
903 for(
size_t i = 0; i < sections.size(); ++i )
905 std::string currSection = sections[i];
907 if( currSection != lastSection && currSection !=
"" )
909 fout <<
"\n[" << currSection <<
"]\n";
912 if( keywords.size() >= i && values.size() >= i )
914 if( keywords[i] !=
"" )
916 fout << keywords[i] <<
"=" << values[i] <<
"\n";
919 lastSection = currSection;
An application configuration manager.
void writeConfigFile(const std::string &fname, const std::vector< std::string > §ions, const std::vector< std::string > &keywords, const std::vector< std::string > &values)
A simple config file writing function, useful for testing.
int(* appConfiguratorParser)(iniFile &parser, const std::string &filename)
Optional parser override used to deterministically test appConfigurator parser failures.
@ filenotfound
The file was not found.
@ parseerr
A parsing error occurred.
@ allocerr
An error occurred during memory allocation.
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
std::unordered_map< std::string, configTarget > m_targets
The targets are stored in an unordered_map for fast access by key.
int readConfig(const std::string &fname, bool reportFileNotFound=true)
Read and parse a config/ini file, updating the targets.
void add(const configTarget &tgt)
Add a configTarget.
bool m_sources
Flag controlling whether or not to record config sources.
int get(typeT &v, const std::string &name, size_t i, std::unordered_map< std::string, configTarget > &targets)
Get the i-th value of the target, converted to the specified type.
std::vector< std::string > nonOptions
Non-option arguments from the command line.
int configUnused(typeT &v, const std::string &key)
Configure a value from the unused map, using the iniFile key.
int verbosity(const std::string &name, std::unordered_map< std::string, configTarget > &targets)
Get the command line verbosity count for this option.
std::unordered_map< std::string, configTarget >::iterator targetIterator
Iterator for the targets unordered_map.
int isSetUnused(const std::string &name)
Check if a target has been set in the unused configuration.
bool isSet(const std::string &name, std::unordered_map< std::string, configTarget > &targets)
Check if a target has been set by the configuration.
void clear()
Clear the containers and free up the associated memory.
int unusedSections(std::vector< std::string > §ions)
Get the unique sections in the unused config targets.
std::list< configTarget > clOnlyTargets
Targets which are only for the command line are stored separately in a list.
int count(const std::string &name, std::unordered_map< std::string, configTarget > &targets)
Get the number of different values set for the specified config target.
void parseCommandLine(int argc, char **argv, const std::string &oneTarget="")
Parse the command line, updating the targets.
int nAdded
Running count of options added, used to track order.
std::unordered_map< std::string, configTarget > m_unusedConfigs
std::list< configTarget >::iterator clOnlyTargetIterator
Iterator for the clOnlyTargets list.
Command line options parser.
void parse(int argc, char **argv, std::vector< std::string > *nonOptions=0)
Parse the command line.
int numUnknown()
Get the number of unknown options found by the parser.
void add(const std::string &optName, const char *const shortOpt, const char *const longOpt, int argT)
Add a command line option target.
int count(const std::string &key)
Get the number of times the option was set on the command line.
unsigned int nOpts
The number of options added.
void getAll(std::vector< std::string > &args, const std::string &key)
Fill a vector of strings with the arguments supplied for key, last to first.
int unknown(std::vector< std::string > &unk)
Get a vector of the unknown options found by the parser.
bool optSet(const std::string &key)
Test whether this option was set.
std::string keyword
The config file keyword, read in a "keyword=value" pair.
std::string section
The config file section name, can be empty "".
std::string name
The name of the target.
A wrapper for the ini functions.
size_t count(const std::string §ion, const std::string &name)
Get the number of entries for the given section and name.
int erase(const std::string §ion, const std::string &name)
Erase the entry for the given section and name.
nameMapT names
The map of section=name keys to values.
static int parseKey(std::string §ion, std::string &name, const std::string &key)
Parse a key into its section and name consituents.
int parse(const std::string &fname)
Calls the inih parse function with this->handler.