aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-03-03 12:05:35 +0100
committertomsmeding <tom.smeding@gmail.com>2017-03-03 12:05:35 +0100
commitb7676f0712f12a68dcf27715addcfb09e27f5b2b (patch)
tree4c645bcb813b10dd56e9b770cceb070c3ec79e77 /main.cpp
parentb86232f77c568e19e9c86b34d1570a4fdc101bee (diff)
Option parsing ++
New syntax: see usage()
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp84
1 files changed, 65 insertions, 19 deletions
diff --git a/main.cpp b/main.cpp
index 99ba3f7..cfa8502 100644
--- a/main.cpp
+++ b/main.cpp
@@ -226,6 +226,26 @@ static void signalHandler(int sig){
#endif
+struct RobotSpec{
+ string fname;
+ bool random;
+ int x,y,heading;
+};
+
+int parseInt(const char *str){
+ char *endp;
+ int v=strtol(str,&endp,10);
+ if(str[0]=='\0'||*endp!='\0'){
+ cerr<<"Invalid number '"<<str<<"'"<<endl;
+ exit(1);
+ }
+ return v;
+}
+
+void usage(char **argv){
+ cerr<<"Usage: "<<argv[0]<<" [-r <fname>]* [-p <fname> <x> <y> <heading>]*"<<endl;
+}
+
int main(int argc,char **argv){
struct timeval tv;
gettimeofday(&tv,nullptr);
@@ -234,31 +254,57 @@ int main(int argc,char **argv){
World world;
vector<Team> teams;
- bool opt_pos = false;
- vector<tuple<int,int,int>> positions;
-
- int k = 1; // Increase past options;
+ vector<RobotSpec> robotSpecs;
- if(argc>k && string(argv[k]) == "-p") {
- opt_pos = true;
- k++;
+ if(argc<=1){
+ usage(argv);
+ return 1;
}
- for(int i=k;i<argc;i++){ // Start after options
- ifstream f(argv[i]);
- assert(f);
- teams.push_back(assemble(preprocess(f)));
- if(opt_pos) {
- positions.emplace_back((int)strtol(argv[i+1],NULL,10), (int)strtol(argv[i+2],NULL,10), (int)strtol(argv[i+3],NULL,10)); // TODO: check if i>argc
- i += 3;
+ for(int i=1;i<argc;i++){
+ if(argv[i][0]!='-'||strlen(argv[i])!=2){
+ cerr<<"Unexpected argument '"<<argv[i]<<"'"<<endl;
+ return 1;
}
- }
+ switch(argv[i][1]){
+ case 'h':
+ usage(argv);
+ return 0;
+
+ case 'r':
+ if(i>=argc-1){
+ cerr<<"Expected file name after '-r'"<<endl;
+ return 1;
+ }
+ robotSpecs.push_back({argv[i+1],true,0,0,0});
+ i+=1;
+ break;
+
+ case 'p':
+ if(i>=argc-4){
+ cerr<<"Expected file name, x, y and heading after '-p'"<<endl;
+ return 1;
+ }
+ robotSpecs.push_back({argv[i+1],false,parseInt(argv[i+2]),parseInt(argv[i+3]),parseInt(argv[i+4])});
+ i+=4;
+ break;
- for(int i=0; i<(int)teams.size(); i++) {
- const Team &t = teams[i];
- Robot &r = opt_pos ? world.create(&t,2,t.banks.size(),false, get<0>(positions[i]), get<1>(positions[i]), get<2>(positions[i]))
- : world.create(&t,2,t.banks.size(),false);
+ default:
+ cerr<<"Unknown switch in '"<<argv[i]<<"'"<<endl;
+ return 1;
+ }
+ }
+ for(const RobotSpec &rs : robotSpecs){
+ ifstream f(rs.fname);
+ if(!f){
+ cerr<<"Cannot open file '"<<rs.fname<<"'"<<endl;
+ return 1;
+ }
+ teams.push_back(assemble(preprocess(f)));
+ Team &t=teams.back();
+ Robot &r=rs.random ? world.create(&t,2,t.banks.size(),false)
+ : world.create(&t,2,t.banks.size(),false,rs.x,rs.y,rs.heading);
for(int i=0;i<(int)t.banks.size();i++){
r.load(i,t.banks[i]);
r.active=1;