From ec6352a5fe1c34a7eff3566ed1bdb5a60db4af49 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Thu, 30 Jun 2016 14:14:59 -0600 Subject: [PATCH] light: option parser: allow negative numbers as option args --- light/light.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/light/light.cc b/light/light.cc index aeed59bc..ead861fe 100644 --- a/light/light.cc +++ b/light/light.cc @@ -1311,12 +1311,21 @@ static void PrintUsage() static bool ParseVec3Optional(vec3_t vec3_out, int *i_inout, int argc, const char **argv) { if ((*i_inout + 3) < argc) { - // negative numbers are rejected, they would be confused with flags. - if (!isdigit(argv[*i_inout + 1][0]) - || !isdigit(argv[*i_inout + 2][0]) - || !isdigit(argv[*i_inout + 3][0])) { - return false; + const int start = (*i_inout + 1); + const int end = (*i_inout + 3); + + // validate that there are 3 numbers + for (int j=start; j <= end; j++) { + if (argv[j][0] == '-' && isdigit(argv[j][1])) { + continue; // accept '-' followed by a digit for negative numbers + } + + // otherwise, reject if the first character is not a digit + if (!isdigit(argv[j][0])) { + return false; + } } + vec3_out[0] = atof( argv[ ++(*i_inout) ] ); vec3_out[1] = atof( argv[ ++(*i_inout) ] ); vec3_out[2] = atof( argv[ ++(*i_inout) ] );