Parsing Command-Line with Options
Feb 15, 2016 at 3:46am UTC
I understand the basic idea of parsing parameters, but I do not understand how to convert the strings into static boolean variables.
The static boolean constants are:
DEBUG -d Debug Mode
DEBUG -debug ^
-o<file>
The Prodived Header File:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#ifndef _OPTIONS_GUARD
#define _OPTIONS_GUARD 1
#include <string>
#include <vector>
//
// An aggregation class for options specified in the command-line
//
class Options
{
public :
Options(int , char **);
virtual bool parseCommandLine();
virtual ~Options() {}
static bool DEBUG;
protected :
int _argc;
char ** _argv;
bool handleOption(int & index);
private :
Options();
};
#endif
The Main Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include "Options.h"
#include "Tokenizer.h"
#include "Token.h"
#include "TestRunner.hpp"
#include "MyVector.hpp"
bool runAllTestFiles(const Options& options)
{
std::string outFile = options.getOutputFile();
std::ofstream ofs(outFile.c_str());
std::ostream& os = outFile.empty() ? std::cout : ofs;
//
// Execute the 'script' for each input file specified on the command-line
//
// TODO
}
int main(int argc, char ** argv)
{
if (argc < 2)
{
std::cerr << "Usage: <program> -debug <input-command-files> -o <output-file>" << std::endl;
return 1;
}
//
// Command-line options object / parsing
//
Options options(argc, argv);
if (!options.parseCommandLine())
{
std::cerr << "Command-line parsing failed; exiting." << std::endl;
return 1;
}
if (Options::DEBUG) std::cout << "Debug flag has been specified." << std::endl;
runAllTestFiles(options);
return 0;
}
Last edited on Feb 15, 2016 at 3:48am UTC
Feb 15, 2016 at 1:49pm UTC
Why is the variable static?
Anyway: You set the variable to true when you detect the appropriate condition (like the presence of 'Debug') otherwise it remains false.
Topic archived. No new replies allowed.