Cope with catcache entries becoming stale during detoasting.
[pgsql.git] / src / fe_utils / option_utils.c
blob763c991015ba72da6bde721379fe2e7bedeac8f7
1 /*-------------------------------------------------------------------------
3 * Command line option processing facilities for frontend code
5 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
6 * Portions Copyright (c) 1994, Regents of the University of California
8 * src/fe_utils/option_utils.c
10 *-------------------------------------------------------------------------
13 #include "postgres_fe.h"
15 #include "common/logging.h"
16 #include "common/string.h"
17 #include "fe_utils/option_utils.h"
20 * Provide strictly harmonized handling of --help and --version
21 * options.
23 void
24 handle_help_version_opts(int argc, char *argv[],
25 const char *fixed_progname, help_handler hlp)
27 if (argc > 1)
29 if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
31 hlp(get_progname(argv[0]));
32 exit(0);
34 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
36 printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
37 exit(0);
43 * option_parse_int
45 * Parse integer value for an option. If the parsing is successful, returns
46 * true and stores the result in *result if that's given; if parsing fails,
47 * returns false.
49 bool
50 option_parse_int(const char *optarg, const char *optname,
51 int min_range, int max_range,
52 int *result)
54 char *endptr;
55 int val;
57 errno = 0;
58 val = strtoint(optarg, &endptr, 10);
61 * Skip any trailing whitespace; if anything but whitespace remains before
62 * the terminating character, fail.
64 while (*endptr != '\0' && isspace((unsigned char) *endptr))
65 endptr++;
67 if (*endptr != '\0')
69 pg_log_error("invalid value \"%s\" for option %s",
70 optarg, optname);
71 return false;
74 if (errno == ERANGE || val < min_range || val > max_range)
76 pg_log_error("%s must be in range %d..%d",
77 optname, min_range, max_range);
78 return false;
81 if (result)
82 *result = val;
83 return true;