File and Configuration Parsing
File format parser
Phoenix2 implements a file parser module called PhoenixFileParser. Its objective is to parse, store, and make accessible all configuration data in a structured manner.
This module supports various file formats and provides a consistent API for accessing configuration parameters, ensuring that applications can easily retrieve and utilize configuration data without worrying about the underlying file format.
Using fileParser you can read configuration files in :
- TOML format using PhoenixToml module.
- YML format using PhoenixYml module.
- XML format using PhoenixXml module.
- JSON format is handled directly by the PhoenixFileParser module.
For example, il you have a configuration file that can handle more than one language for it's description, you can implement a function that call the correct parser :
void load_config(PLog & log, ConfigNode & dico, const PString & inputConfig, ConfigFormat::ConfigFormat format){
if(format == ConfigFormat::JSON){
if(!parser_jsonString(dico, inputConfig)){
log.criticalAndThrow<Phoenix::ParserException>("load_config", "cannot load config from input string");
}
}else if(format == ConfigFormat::TOML){
if(!parser_tomlString(dico, inputConfig)){
log.criticalAndThrow<Phoenix::ParserException>("load_config", "cannot load config from input string");
}
}else if(format == ConfigFormat::YAML){
if(!parser_ymlString(dico, inputConfig)){
log.criticalAndThrow<Phoenix::ParserException>("load_config", "cannot load config from input string");
}
}
CLI handler and parser
Phoenix2 also provides tools to generate CLI clients for command line executables. If you have developped a C++ program that needs to be run in terminal, you can use PhoenixOptionParser. Using it you can manage to create a CLI for your executable, specifying modes, parameters and arguments.
/// Creating a base option parser
OptionParser parser(true, __PROGRAM_VERSION__);
parser.setExampleLongOption("myExecutable A --optionA=some_option");
/// Mode A
parser.addMode("A");
parser.addOption("optionA", "a", OptionType::NONE, false, "Option A is used to set option A");
parser.addOption("optionb", "b", OptionType::NONE, false, "Option B is used to set option b");
parser.closeMode();
/// Mode B
parser.addMode("B");
parser.addOption("optionA", "a", OptionType::NONE, false, "Option A is used to set option A");
parser.addOption("optionb", "b", OptionType::NONE, false, "Option B is used to set option b");
parser.closeMode();
return parser;
}
Then with your executable you will be able to call this to run A mode with b option