fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
options.c
(4057B)
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 " -m, --multi Enable multi-selection\n"
24 " -n, --no-color Do not use colored highlighting\n"
25 " -h, --help Display this help and exit\n"
26 " -v, --version Output version information and exit\n";
27
28 static void usage(const char *argv0) {
29 fprintf(stderr, usage_str, argv0);
30 }
31
32 static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'},
33 {"query", required_argument, NULL, 'q'},
34 {"lines", required_argument, NULL, 'l'},
35 {"tty", required_argument, NULL, 't'},
36 {"prompt", required_argument, NULL, 'p'},
37 {"show-scores", no_argument, NULL, 's'},
38 {"read-null", no_argument, NULL, '0'},
39 {"version", no_argument, NULL, 'v'},
40 {"benchmark", optional_argument, NULL, 'b'},
41 {"workers", required_argument, NULL, 'j'},
42 {"show-info", no_argument, NULL, 'i'},
43 {"multi", no_argument, NULL, 'm'},
44 {"no-color", no_argument, NULL, 'n'},
45 {"help", no_argument, NULL, 'h'},
46 {NULL, 0, NULL, 0}};
47
48 void options_init(options_t *options) {
49 /* set defaults */
50 options->benchmark = 0;
51 options->filter = NULL;
52 options->init_search = NULL;
53 options->show_scores = 0;
54 options->tty_filename = DEFAULT_TTY;
55 options->num_lines = DEFAULT_NUM_LINES;
56 options->prompt = DEFAULT_PROMPT;
57 options->workers = DEFAULT_WORKERS;
58 options->input_delimiter = '\n';
59 options->show_info = DEFAULT_SHOW_INFO;
60 options->multi = 0;
61 options->no_color = 0;
62 }
63
64 void options_parse(options_t *options, int argc, char *argv[]) {
65 options_init(options);
66
67 int c;
68 while ((c = getopt_long(argc, argv, "vhs0mne:q:l:t:p:j:i", longopts, NULL)) != -1) {
69 switch (c) {
70 case 'v':
71 printf("%s " VERSION " © 2014-2025 John Hawthorn\n", argv[0]);
72 exit(EXIT_SUCCESS);
73 case 's':
74 options->show_scores = 1;
75 break;
76 case '0':
77 options->input_delimiter = '\0';
78 break;
79 case 'q':
80 options->init_search = optarg;
81 break;
82 case 'e':
83 options->filter = optarg;
84 break;
85 case 'b':
86 if (optarg) {
87 if (sscanf(optarg, "%d", &options->benchmark) != 1) {
88 usage(argv[0]);
89 exit(EXIT_FAILURE);
90 }
91 } else {
92 options->benchmark = 100;
93 }
94 break;
95 case 't':
96 options->tty_filename = optarg;
97 break;
98 case 'p':
99 options->prompt = optarg;
100 break;
101 case 'j':
102 if (sscanf(optarg, "%u", &options->workers) != 1) {
103 usage(argv[0]);
104 exit(EXIT_FAILURE);
105 }
106 break;
107 case 'l': {
108 int l;
109 if (!strcmp(optarg, "max")) {
110 l = INT_MAX;
111 } else if (sscanf(optarg, "%d", &l) != 1 || l < 3) {
112 fprintf(stderr, "Invalid format for --lines: %s\n", optarg);
113 fprintf(stderr, "Must be integer in range 3..\n");
114 usage(argv[0]);
115 exit(EXIT_FAILURE);
116 }
117 options->num_lines = l;
118 } break;
119 case 'i':
120 options->show_info = 1;
121 break;
122 case 'm':
123 options->multi = 1;
124 break;
125 case 'n':
126 options->no_color = 1;
127 break;
128 case 'h':
129 default:
130 usage(argv[0]);
131 exit(EXIT_SUCCESS);
132 }
133 }
134 if (optind != argc) {
135 usage(argv[0]);
136 exit(EXIT_FAILURE);
137 }
138 }