aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorShav Kinderlehrer <[email protected]>2023-04-13 09:03:33 -0400
committerShav Kinderlehrer <[email protected]>2023-04-13 09:03:33 -0400
commit5f01703842daf79a5beb9b2d87bf42034346de03 (patch)
tree652ef2af1318c015954edbc1fe5d70ba8e2812a6 /src
parent58e0d435c38c0b4e6c59244436556dba6ed96abf (diff)
downloadlat-5f01703842daf79a5beb9b2d87bf42034346de03.tar.gz
lat-5f01703842daf79a5beb9b2d87bf42034346de03.zip
Add argsv0.5.0
- --help - --version
Diffstat (limited to 'src')
-rw-r--r--src/lib/arg.c47
-rw-r--r--src/main.c16
2 files changed, 54 insertions, 9 deletions
diff --git a/src/lib/arg.c b/src/lib/arg.c
index 6178156..4b98f2b 100644
--- a/src/lib/arg.c
+++ b/src/lib/arg.c
@@ -4,9 +4,25 @@
#include <stdlib.h>
#include <string.h>
+#define LAT_USAGE "usage: lat [cnVh] [files..]"
+
+void help(void) {
+ printf("%s\n", LAT_USAGE);
+ printf("options:\n"
+ "\t-c, --color\t toggle whether to print color or not\n"
+ "\t-n, --lines\t toggle whether to print line numbers or not\n"
+ "\t-V, --version\t show program version\n"
+ "\t-h, --help\t display this help text\n");
+}
+
+void version(void) {
+ printf("lat - v%s | %s:%s\n", LAT_VERSION, __DATE__, __TIME__);
+}
+
struct config conf;
void argerr(char *r, char *arg) {
- fprintf(stderr, "lat: %s '%s'\n", r, arg);
+ printf("lat: %s '%s'\n", r, arg);
+ printf("%s\n", LAT_USAGE);
exit(EXIT_FAILURE);
}
@@ -21,6 +37,18 @@ void parselongarg(char *arg) {
return;
}
+ if (strcmp(arg, "--help") == 0) {
+ help();
+ exit(EXIT_SUCCESS);
+ return;
+ }
+
+ if (strcmp(arg, "--version") == 0) {
+ version();
+ exit(EXIT_SUCCESS);
+ return;
+ }
+
argerr("unrecognized arg", arg);
}
@@ -35,9 +63,22 @@ void parseshortarg(char *arg) {
case 'n':
conf.lines = !conf.lines;
break;
- default:
- argerr("unrecognized flag", &c);
+ case 'h':
+ help();
+ exit(EXIT_SUCCESS);
break;
+ case 'V':
+ version();
+ exit(EXIT_SUCCESS);
+ break;
+ default: {
+ char *str = malloc(2);
+ str[0] = c;
+ str[1] = '\0';
+ argerr("unrecognized flag", str);
+ free(str);
+ break;
+ }
}
i++;
}
diff --git a/src/main.c b/src/main.c
index dc4d5f8..59b0849 100644
--- a/src/main.c
+++ b/src/main.c
@@ -23,9 +23,9 @@ void run(FILE *fp, char *filename, int tty) {
f = readfile(fp);
if (tty) {
- char *addon = f.binary ? " <binary>" : "";
- fprintf(stderr, "\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
- uinvert_t);
+ char *addon = f.binary ? "<binary>" : "";
+ fprintf(stderr, "\r\x1b[2K%s%s%s%s\r\n", invert_t, basename(filename),
+ addon, uinvert_t);
}
int lcpad = intlen(f.lc);
@@ -82,7 +82,7 @@ int main(int argc, char *argv[]) {
if (argc > 1) {
int offset = parseargs(argc, argv);
- for (int i = offset; i < argc; i++) { // start at one to offset argv[0]
+ for (int i = offset; i < argc; i++) {
FILE *fp = fopen(argv[i], "rb");
if (fp == NULL)
die(argv[i]);
@@ -91,10 +91,14 @@ int main(int argc, char *argv[]) {
run(fp, argv[i], tty);
fclose(fp);
- if (i + 1 != argc) {
- fprintf(stderr, "\r\n"); // separate concurrent files
+ if (tty && (i + 1 != argc)) {
+ fprintf(stderr, "\r\n"); // separate concurrent files in tty
}
}
+
+ if (offset == argc) {
+ run(stdin, "stdin", 1);
+ }
} else {
run(stdin, "stdin", 1); // for piped-input or repl-like behavior
}