int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; if (PROGRAM_NAME == NULL) { PROGRAM_NAME = get_progname(argv[0]); set_pglocale_pgservice(argv[0], "pgscripts"); } /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit_or_abort(HELP); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit_or_abort(HELP); } } /* Merge default and user options. */ longopts = option_merge(default_options, options); optstring = longopts_to_optstring(longopts); /* Assign named options */ while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1) { opt = option_find(c, default_options, options); assign_option(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); (void) (dbname || (dbname = getenv("PGDATABASE")) || (dbname = getenv("PGUSER")) || (dbname = get_username())); init_cancel_handler(); atexit(on_cleanup); return optind; }
int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; pgut_init(argc, argv); /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit(1); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { printf("%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } if (strcmp(argv[1], "--configuration") == 0) { printf("%s\n", PG_VERSION_STR); exit(0); } } /* Merge default and user options. */ longopts = option_merge(default_options, options); optstring = longopts_to_optstring(longopts); /* Assign named options */ while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1) { opt = option_find(c, default_options, options); pgut_setopt(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); (void) (dbname || (dbname = getenv("PGDATABASE")) || (dbname = getenv("PGUSER")) || (dbname = get_username())); return optind; }
int pgut_getopt(int argc, char **argv, pgut_option options[]) { int c; int optindex = 0; char *optstring; struct option *longopts; pgut_option *opt; pgut_init(argc, argv); /* Help message and version are handled at first. */ if (argc > 1) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) { help(true); exit(1); } if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } } /* Merge default and user options. */ longopts = option_merge(default_options, options); optstring = longopts_to_optstring(longopts); /* Assign named options */ while ((c = getopt_long(argc, argv, optstring, longopts, &optindex)) != -1) { /* Show help message if are specified '-?, --help' */ if (c == '?') { /* Actual help option given */ if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0) { help(true); exit(1); } /* unknown option reported by getopt */ else { fprintf(stderr, "Try \"%s --help\" for more information.\n", PROGRAM_NAME); exit(EINVAL); } } /* Show version information if are specified '-V, --version' */ if (c == 'V') { fprintf(stderr, "%s %s\n", PROGRAM_NAME, PROGRAM_VERSION); exit(1); } opt = option_find(c, default_options, options); pgut_setopt(opt, optarg, SOURCE_CMDLINE); } /* Read environment variables */ option_from_env(options); return optind; }