aboutsummaryrefslogtreecommitdiff
path: root/ask.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-06-04 20:26:31 -0700
committerBruce Hill <bruce@bruce-hill.com>2019-06-04 20:26:31 -0700
commit904af4e28d8f73329eaf25bcf0693f53953c47d0 (patch)
tree0e1ebc5b2f7fd04a75b49683354c76480ce24348 /ask.c
parentb4bf9255453ae88ef54eddc00bd752a43b32ae26 (diff)
Improved arg parsing and moved initial input to command line flag.
Diffstat (limited to 'ask.c')
-rw-r--r--ask.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/ask.c b/ask.c
index 64822db..2a46ac6 100644
--- a/ask.c
+++ b/ask.c
@@ -1,9 +1,12 @@
/* ask - a simple command line asker
* Copyright 2019 Bruce Hill
* Released under the MIT License (see LICENSE for details)
- * Usage: ask [-p | --password] [-q | --quickpick] [prompt [initial value]]
- * -p: password mode
- * -q: quickpick mode (exit when only one option remains)
+ * Usage: ask [-q|--quickpick] [-p|--password] [-v|--version] [-h|--help] [--initial=initial] [prompt [options...]]
+ * --password: password mode
+ * --quickpick: quickpick mode (exit when only one option remains)
+ * --version: print version and exit
+ * --help: print usage and exit
+ * --initial: initial value to pre-populate user input
*/
#include <poll.h>
#include <stdio.h>
@@ -118,10 +121,11 @@ static char *get_input(FILE *in, FILE *out, const char *prompt, const char *init
bestscore = s;
}
}
- if (quickpick && ncandidates == 1 && best)
- return best;
picked = best;
+ if (quickpick && ncandidates == 1 && best)
+ goto finished;
+
if (password) {
if (best) fputs("\033[0;32m", out);
else if (nopts > 0) fputs("\033[0;31m", out);
@@ -246,15 +250,28 @@ int main(int argc, char *argv[])
}
int a = 1;
while (a < argc) {
- if (strcmp(argv[a], "-p") == 0 || strcmp(argv[a], "--password") == 0) {
+ if (argv[a][0] == '-' && argv[a][1] != '-') {
+ for (char *p = &argv[a][1]; *p; p++) {
+ switch (*p) {
+ case 'p': password = 1; break;
+ case 'q': quickpick = 1; break;
+ case 'h': goto help_flag;
+ case 'v': goto version_flag;
+ }
+ }
+ } else if (strncmp(argv[a], "--initial=", strlen("--initial=")) == 0) {
+ initial = &argv[a][strlen("--initial=")];
+ } else if (strcmp(argv[a], "--password") == 0) {
password = 1;
- } else if (strcmp(argv[a], "-q") == 0 || strcmp(argv[a], "--quickpick") == 0) {
+ } else if (strcmp(argv[a], "--quickpick") == 0) {
quickpick = 1;
- } else if (strcmp(argv[a], "-h") == 0 || strcmp(argv[a], "--help") == 0) {
+ } else if (strcmp(argv[a], "--help") == 0) {
+ help_flag:
printf("ask - A simple command line input tool.\n"
- "Usage: ask [-q|--quickpick] [-p|--password] [-v|--version] [-h|--help] [prompt [initial [options...]]]\n");
+ "Usage: ask [-q|--quickpick] [-p|--password] [-v|--version] [-h|--help] [--initial=initial] [prompt [options...]]\n");
return 0;
- } else if (strcmp(argv[a], "-v") == 0 || strcmp(argv[a], "--version") == 0) {
+ } else if (strcmp(argv[a], "--version") == 0) {
+ version_flag:
printf("ask v0.1\n");
return 0;
} else break;
@@ -262,7 +279,6 @@ int main(int argc, char *argv[])
}
if (!prompt && a < argc) prompt = argv[a++];
if (!prompt) prompt = "> ";
- if (!initial && a < argc) initial = argv[a++];
while (a < argc) {
if ((size_t)nlines >= linescap)