Woah - slow down there! You have a strongly typed language. This is not a design flaw, it is intentional: it should be a little limited, so it can do compile-time checks on the correctness of your program and produce much faster, cleaner code. Please do not go out of your way to throw away type safety by creating some kind of ambiguous interface! There is nothing in your question to suggest that you need to do this.
Consider doing something like:
struct Config { int max_for_whatever_; string name_for_whatever_; double scaling_factor_for_whatever_else_; bool verbose_; };
When analyzing inputs, you can populate a specific associated member variable.
Now there are many good libraries for this. The leading universal C ++ third-party library is "boost", which has argument analysis tools. And although some C ++ compilers come with extended versions of this (in particular, the GNU C ++ compiler has an extended getopt supporting a "long" format of options for command line options such as "--flag", and not just "-f" ), a proven and reliable UNIX / Linux object getopt() can be used as:
int c; Config config = { 20, "plugins", 29.3, true }; while ((c = getopt(argc, argv, "m:n:s:v")) != EOF) { switch (c) { case 'm': config.max_for_whatever_ = lexical_cast<int>(optarg); break; case 'n': config.name_for_whatever_ = optarg; break; case 'd': config.scaling_factor_for_whatever_ = lexical_cast<double>(optarg); break; case 'v': config.verbose_ ^= true; break; default: std::cerr << argv[0] << ": unsupported option '" << c << "' - exiting\n"; exit(EXIT_FAILURE); } }
The concepts are the same, regardless of whether you read from the configuration file, command line arguments, or in some way the registry: since you are faced with certain configuration values, try writing them to correctly typed and labeled variables specific for importing them into code.
source share