-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sian Cao
committed
Apr 24, 2014
1 parent
5f4ffbc
commit d85bbcb
Showing
3 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <unistd.h> | ||
#include <getopt.h> | ||
#include <iostream> | ||
|
||
using namespace std; | ||
#include "options.h" | ||
|
||
OptionManager* OptionManager::_instance = nullptr; | ||
|
||
OptionManager::OptionManager() | ||
{ | ||
} | ||
|
||
void OptionManager::usage() | ||
{ | ||
cerr << "usage: " << _progName << " [-m [text|scene]] [-h]" << endl; | ||
exit(EXIT_FAILURE); | ||
} | ||
|
||
void OptionManager::parse(int argc, char *argv[]) | ||
{ | ||
_progName = {argv[0]}; | ||
if (_progName.find("./") == 0) { | ||
_progName.erase(_progName.begin(), _progName.begin()+1); | ||
} | ||
|
||
struct option opts[] = { | ||
{"mode", 1, NULL, 0}, | ||
{NULL, 0, NULL, 0} | ||
}; | ||
|
||
int c, index; | ||
while ((c = getopt_long(argc, argv, "m:h", opts, &index)) != -1) { | ||
switch(c) { | ||
case 'm': _mode = {optarg}; break; | ||
case 'h': usage(); break; | ||
default: break; | ||
} | ||
} | ||
|
||
} | ||
|
||
OptionManager* OptionManager::get(int argc, char *argv[]) | ||
{ | ||
if (!_instance) { | ||
_instance = new OptionManager; | ||
_instance->parse(argc, argv); | ||
|
||
} | ||
|
||
return _instance; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef _OPTIONS_H | ||
#define _OPTIONS_H | ||
|
||
#include <type_traits> | ||
|
||
class OptionManager { | ||
public: | ||
static OptionManager* get(int argc, char *argv[]); | ||
template <typename T> | ||
T get(string opt); | ||
|
||
private: | ||
static OptionManager* _instance; | ||
string _progName; | ||
string _mode; | ||
|
||
OptionManager(); | ||
void parse(int argc, char *argv[]); | ||
void usage(); | ||
}; | ||
|
||
|
||
template <typename T> | ||
T OptionManager::get(string opt) | ||
{ | ||
if (opt == "mode") | ||
return _mode; | ||
|
||
return ""; | ||
} | ||
|
||
#endif |