JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Removed unnecessary command-line options.
[vor.git] / args.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "args.h"
5 #include "config.h"
6
7 // Gameplay Variations
8 float opt_bounciness;
9 float opt_gamespeed;
10 float opt_max_lead;
11
12 // Look and Feel
13 int opt_fullscreen;
14 int opt_sound;
15
16 static void
17 show_help(void)
18 {
19         puts("Dodge the rocks until you die.");
20         putchar('\n');
21         puts("  -f, --full-screen");
22         puts("  -s, --silent               No explosion sounds or music");
23         puts("  -V, --version              Print program version");
24         puts("  -?, --help                 Give this help list");
25         putchar('\n');
26         puts("Mandatory or optional arguments to long options are also mandatory or optional");
27         puts("for any corresponding short options.");
28         putchar('\n');
29         puts("Report bugs at http://jasonwoof.com/contact.html");
30 }
31
32 int
33 short_opt(char c, char *arg)
34 {
35         switch(c) {
36                 case 'f': opt_fullscreen = 1; break;
37                 case 's': opt_sound = 0; break;
38                 case 'V':
39                                   printf("Variations on Rockdodger %s\n", VERSION);
40                                   exit(0);
41                 case '?':
42                 case 'h': return 0;
43                 default: 
44                                   fprintf(stderr, "unknown option -%c\n\n", c);
45                                   return 0;
46         }
47         return 1;
48 }
49
50 int
51 parse_short_opts(const char *s, char *arg)
52 {
53         while(s[1]) if(!short_opt(*s++, NULL)) return 0;
54         return short_opt(*s, arg);
55 }
56
57 static char *long_opts[] = { "full-screen", "silent", "version", "help" };
58
59 static char short_opts[] = { 'f', 's', 'V', 'h' };
60
61 int
62 parse_long_opt(const char *s, char *arg)
63 {
64         int i;
65         for(i=0; i<sizeof(short_opts); i++) {
66                 if(strcmp(s, long_opts[i]) == 0)
67                         return short_opt(short_opts[i], arg);
68         }
69         fprintf(stderr, "unknown long option --%s\n\n", s);
70         return 0;
71 }
72
73 void
74 init_opts(void)
75 {
76         // Gameplay Variations
77         opt_bounciness = 0.50; // lose 50% when you hit the screen edge.
78         opt_gamespeed = 1.00; // Run game at full speed.
79         opt_max_lead = 1.00*XSIZE;  // you can get 1 screen ahead.
80
81         // Look and Feel
82         opt_fullscreen = 0;
83         opt_sound = 1;
84 }
85
86 int
87 parse_opts(int argc, char *argv[])
88 {
89         int i;
90         char *r;
91
92         init_opts();
93         for(i=1; i<argc; i++) {
94                 char *s, *arg;
95                 s = argv[i]; if(!*s) continue;
96                 if(*s++ != '-') {
97                         fputs("not an option\n\n", stderr);
98                         show_help();
99                         return 0;
100                 }
101
102                 arg = NULL;
103                 for(r=s; *r; r++) if(*r == '=') { *r = 0; arg = r+1; break; }
104                 if(!arg && (i+1 < argc)) arg = argv[i+1];
105
106                 if(*s == '-') {
107                         if(!parse_long_opt(s+1, arg)) { show_help(); return 0; }
108                 } else {
109                    if(!parse_short_opts(s, arg)) { show_help(); return 0; }
110                 }
111         }
112         return 1;
113 }