48 |
|
* Initialize preferences |
49 |
|
*/ |
50 |
|
|
51 |
< |
void PrefsInit(int argc, char **argv) |
51 |
> |
void PrefsInit(int &argc, char **&argv) |
52 |
|
{ |
53 |
|
// Set defaults |
54 |
|
AddPrefsDefaults(); |
55 |
|
|
56 |
< |
// Override prefs with command line arguments |
57 |
< |
argc--; argv++; |
58 |
< |
for (; argc>0; argc--, argv++) { |
56 |
> |
// Override prefs with command line options |
57 |
> |
for (int i=1; i<argc; i++) { |
58 |
|
|
59 |
< |
// Arguments are of the form '--keyword' |
60 |
< |
if (strlen(*argv) < 3 || argv[0][0] != '-' || argv[0][1] != '-') |
59 |
> |
// Options are of the form '--keyword' |
60 |
> |
const char *option = argv[i]; |
61 |
> |
if (strlen(option) < 3 || option[0] != '-' || option[1] != '-') |
62 |
|
continue; |
63 |
< |
const char *keyword = *argv + 2; |
63 |
> |
const char *keyword = option + 2; |
64 |
|
|
65 |
|
// Find descriptor for keyword |
66 |
|
const prefs_desc *d = find_prefs_desc(keyword); |
67 |
< |
bool negated_bool = false; |
68 |
< |
if (d == NULL) { |
69 |
< |
// We also accept boolean arguments in the form '--nokeyword' |
70 |
< |
if (strncmp(keyword, "no", 2) == 0) { |
71 |
< |
negated_bool = true; |
72 |
< |
keyword += 2; |
73 |
< |
d = find_prefs_desc(keyword); |
74 |
< |
} |
75 |
< |
if (d == NULL) { |
76 |
< |
printf("WARNING: Unrecognized argument '%s'\n", *argv); |
77 |
< |
continue; |
78 |
< |
} |
67 |
> |
if (d == NULL) |
68 |
> |
continue; |
69 |
> |
argv[i] = NULL; |
70 |
> |
|
71 |
> |
// Get value |
72 |
> |
i++; |
73 |
> |
if (i >= argc) { |
74 |
> |
fprintf(stderr, "Option '%s' must be followed by a value\n", option); |
75 |
> |
continue; |
76 |
|
} |
77 |
+ |
const char *value = argv[i]; |
78 |
+ |
argv[i] = NULL; |
79 |
|
|
80 |
|
// Add/replace prefs item |
81 |
|
switch (d->type) { |
82 |
|
case TYPE_STRING: |
84 |
– |
if (argc < 2) { |
85 |
– |
printf("WARNING: Argument '%s' must be followed by value\n", *argv); |
86 |
– |
break; |
87 |
– |
} |
88 |
– |
argc--; argv++; |
83 |
|
if (d->multiple) |
84 |
< |
PrefsAddString(keyword, *argv); |
84 |
> |
PrefsAddString(keyword, value); |
85 |
|
else |
86 |
< |
PrefsReplaceString(keyword, *argv); |
86 |
> |
PrefsReplaceString(keyword, value); |
87 |
|
break; |
88 |
|
|
89 |
|
case TYPE_BOOLEAN: { |
90 |
< |
bool new_value; |
91 |
< |
if (negated_bool) |
92 |
< |
new_value = false; |
93 |
< |
else if (argc > 1 && argv[1][0] != '-') { |
94 |
< |
if (!strcmp(argv[1], "true") || !strcmp(argv[1], "on") || !strcmp(argv[1], "yes")) { |
95 |
< |
new_value = true; |
102 |
< |
argc--; argv++; |
103 |
< |
} else if (!strcmp(argv[1], "false") || !strcmp(argv[1], "off") || !strcmp(argv[1], "no")) { |
104 |
< |
new_value = false; |
105 |
< |
argc--; argv++; |
106 |
< |
} else |
107 |
< |
new_value = true; |
108 |
< |
} else |
109 |
< |
new_value = true; |
110 |
< |
PrefsReplaceBool(keyword, new_value); |
90 |
> |
if (!strcmp(value, "true") || !strcmp(value, "on") || !strcmp(value, "yes")) |
91 |
> |
PrefsReplaceBool(keyword, true); |
92 |
> |
else if (!strcmp(value, "false") || !strcmp(value, "off") || !strcmp(value, "no")) |
93 |
> |
PrefsReplaceBool(keyword, false); |
94 |
> |
else |
95 |
> |
fprintf(stderr, "Value for option '%s' must be 'true' or 'false'\n", option); |
96 |
|
break; |
97 |
|
} |
98 |
|
|
99 |
|
case TYPE_INT32: |
100 |
< |
if (argc < 2) { |
116 |
< |
printf("WARNING: Argument '%s' must be followed by value\n", *argv); |
117 |
< |
break; |
118 |
< |
} |
119 |
< |
argc--; argv++; |
120 |
< |
PrefsReplaceInt32(keyword, atoi(*argv)); |
100 |
> |
PrefsReplaceInt32(keyword, atoi(value)); |
101 |
|
break; |
102 |
|
|
103 |
|
default: |
104 |
|
break; |
105 |
|
} |
106 |
|
} |
107 |
+ |
|
108 |
+ |
// Remove processed arguments |
109 |
+ |
for (int i=1; i<argc; i++) { |
110 |
+ |
int k; |
111 |
+ |
for (k=i; k<argc; k++) |
112 |
+ |
if (argv[k] != NULL) |
113 |
+ |
break; |
114 |
+ |
if (k > i) { |
115 |
+ |
k -= i; |
116 |
+ |
for (int j=i+k; j<argc; j++) |
117 |
+ |
argv[j-k] = argv[j]; |
118 |
+ |
argc -= k; |
119 |
+ |
} |
120 |
+ |
} |
121 |
|
} |
122 |
|
|
123 |
|
|
183 |
|
{ |
184 |
|
printf("\nGeneral options:\n"); |
185 |
|
print_options(common_prefs_items); |
186 |
< |
printf("\nBoolean options can be specified as '--OPTION true|on|yes' and\n'--OPTION false|off|no', or as '--OPTION' and '--noOPTION'.\n"); |
186 |
> |
printf("\nBoolean options are specified as '--OPTION true|on|yes' or\n'--OPTION false|off|no'.\n"); |
187 |
|
} |
188 |
|
|
189 |
|
|