diff options
Diffstat (limited to 'src/fe_utils/option_utils.c')
-rw-r--r-- | src/fe_utils/option_utils.c | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/fe_utils/option_utils.c b/src/fe_utils/option_utils.c index e19a495dba7..3e7e512ad91 100644 --- a/src/fe_utils/option_utils.c +++ b/src/fe_utils/option_utils.c @@ -12,6 +12,8 @@ #include "postgres_fe.h" +#include "common/logging.h" +#include "common/string.h" #include "fe_utils/option_utils.h" /* @@ -36,3 +38,40 @@ handle_help_version_opts(int argc, char *argv[], } } } + +/* + * option_parse_int + * + * Parse integer value for an option. If the parsing is successful, returns + * true and stores the result in *result if that's given; if parsing fails, + * returns false. + */ +bool +option_parse_int(const char *optarg, const char *optname, + int min_range, int max_range, + int *result) +{ + char *endptr; + int val; + + errno = 0; + val = strtoint(optarg, &endptr, 10); + + if (*endptr) + { + pg_log_error("invalid value \"%s\" for option %s", + optarg, optname); + return false; + } + + if (errno == ERANGE || val < min_range || val > max_range) + { + pg_log_error("%s must be in range %d..%d", + optname, min_range, max_range); + return false; + } + + if (result) + *result = val; + return true; +} |