systemSetup

system setup, configuration and dotfiles
git clone https://noulin.net/git/systemSetup.git
Log | Files | Refs | README | LICENSE

options.c (3712B)


      1 #include <getopt.h>
      2 #include <limits.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "options.h"
      8 
      9 #include "config.h"
     10 
     11 static const char *usage_str =
     12     ""
     13     "Usage: fzy [OPTION]...\n"
     14     " -l, --lines=LINES        Specify how many lines of results to show (default 10)\n"
     15     " -p, --prompt=PROMPT      Input prompt (default '> ')\n"
     16     " -q, --query=QUERY        Use QUERY as the initial search string\n"
     17     " -e, --show-matches=QUERY Output the sorted matches of QUERY\n"
     18     " -t, --tty=TTY            Specify file to use as TTY device (default /dev/tty)\n"
     19     " -s, --show-scores        Show the scores of each match\n"
     20     " -0, --read-null          Read input delimited by ASCII NUL characters\n"
     21     " -j, --workers NUM        Use NUM workers for searching. (default is # of CPUs)\n"
     22     " -i, --show-info          Show selection info line\n"
     23     " -h, --help     Display this help and exit\n"
     24     " -v, --version  Output version information and exit\n";
     25 
     26 static void usage(const char *argv0) {
     27 	fprintf(stderr, usage_str, argv0);
     28 }
     29 
     30 static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
     31 				   {"query", required_argument, NULL, 'q'},
     32 				   {"lines", required_argument, NULL, 'l'},
     33 				   {"tty", required_argument, NULL, 't'},
     34 				   {"prompt", required_argument, NULL, 'p'},
     35 				   {"show-scores", no_argument, NULL, 's'},
     36 				   {"read-null", no_argument, NULL, '0'},
     37 				   {"version", no_argument, NULL, 'v'},
     38 				   {"benchmark", optional_argument, NULL, 'b'},
     39 				   {"workers", required_argument, NULL, 'j'},
     40 				   {"show-info", no_argument, NULL, 'i'},
     41 				   {"help", no_argument, NULL, 'h'},
     42 				   {NULL, 0, NULL, 0}};
     43 
     44 void options_init(options_t *options) {
     45 	/* set defaults */
     46 	options->benchmark       = 0;
     47 	options->filter          = NULL;
     48 	options->init_search     = NULL;
     49 	options->show_scores     = 0;
     50 	options->scrolloff       = 1;
     51 	options->tty_filename    = DEFAULT_TTY;
     52 	options->num_lines       = DEFAULT_NUM_LINES;
     53 	options->prompt          = DEFAULT_PROMPT;
     54 	options->workers         = DEFAULT_WORKERS;
     55 	options->input_delimiter = '\n';
     56 	options->show_info       = DEFAULT_SHOW_INFO;
     57 }
     58 
     59 void options_parse(options_t *options, int argc, char *argv[]) {
     60 	options_init(options);
     61 
     62 	int c;
     63 	while ((c = getopt_long(argc, argv, "vhs0e:q:l:t:p:j:i", longopts, NULL)) != -1) {
     64 		switch (c) {
     65 			case 'v':
     66 				printf("%s " VERSION " © 2014-2018 John Hawthorn\n", argv[0]);
     67 				exit(EXIT_SUCCESS);
     68 			case 's':
     69 				options->show_scores = 1;
     70 				break;
     71 			case '0':
     72 				options->input_delimiter = '\0';
     73 				break;
     74 			case 'q':
     75 				options->init_search = optarg;
     76 				break;
     77 			case 'e':
     78 				options->filter = optarg;
     79 				break;
     80 			case 'b':
     81 				if (optarg) {
     82 					if (sscanf(optarg, "%d", &options->benchmark) != 1) {
     83 						usage(argv[0]);
     84 						exit(EXIT_FAILURE);
     85 					}
     86 				} else {
     87 					options->benchmark = 100;
     88 				}
     89 				break;
     90 			case 't':
     91 				options->tty_filename = optarg;
     92 				break;
     93 			case 'p':
     94 				options->prompt = optarg;
     95 				break;
     96 			case 'j':
     97 				if (sscanf(optarg, "%u", &options->workers) != 1) {
     98 					usage(argv[0]);
     99 					exit(EXIT_FAILURE);
    100 				}
    101 				break;
    102 			case 'l': {
    103 				int l;
    104 				if (!strcmp(optarg, "max")) {
    105 					l = INT_MAX;
    106 				} else if (sscanf(optarg, "%d", &l) != 1 || l < 3) {
    107 					fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
    108 					fprintf(stderr, "Must be integer in range 3..\n");
    109 					usage(argv[0]);
    110 					exit(EXIT_FAILURE);
    111 				}
    112 				options->num_lines = l;
    113 			} break;
    114 			case 'i':
    115 				options->show_info = 1;
    116 				break;
    117 			case 'h':
    118 			default:
    119 				usage(argv[0]);
    120 				exit(EXIT_SUCCESS);
    121 		}
    122 	}
    123 	if (optind != argc) {
    124 		usage(argv[0]);
    125 		exit(EXIT_FAILURE);
    126 	}
    127 }