- --help
- --version
This commit is contained in:
Shav Kinderlehrer 2023-04-13 09:03:33 -04:00
parent 58e0d435c3
commit 5f01703842
3 changed files with 56 additions and 9 deletions

View File

@ -2,6 +2,8 @@
#define ARG_H #define ARG_H
#include <stdbool.h> #include <stdbool.h>
#define LAT_VERSION "0.4.0"
struct config { struct config {
bool color; bool color;
bool lines; bool lines;

View File

@ -4,9 +4,25 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.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; struct config conf;
void argerr(char *r, char *arg) { 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); exit(EXIT_FAILURE);
} }
@ -21,6 +37,18 @@ void parselongarg(char *arg) {
return; 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); argerr("unrecognized arg", arg);
} }
@ -35,9 +63,22 @@ void parseshortarg(char *arg) {
case 'n': case 'n':
conf.lines = !conf.lines; conf.lines = !conf.lines;
break; break;
default: case 'h':
argerr("unrecognized flag", &c); help();
exit(EXIT_SUCCESS);
break; 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++; i++;
} }

View File

@ -23,9 +23,9 @@ void run(FILE *fp, char *filename, int tty) {
f = readfile(fp); f = readfile(fp);
if (tty) { if (tty) {
char *addon = f.binary ? " <binary>" : ""; char *addon = f.binary ? "<binary>" : "";
fprintf(stderr, "\r%s%s%s%s\r\n", invert_t, basename(filename), addon, fprintf(stderr, "\r\x1b[2K%s%s%s%s\r\n", invert_t, basename(filename),
uinvert_t); addon, uinvert_t);
} }
int lcpad = intlen(f.lc); int lcpad = intlen(f.lc);
@ -82,7 +82,7 @@ int main(int argc, char *argv[]) {
if (argc > 1) { if (argc > 1) {
int offset = parseargs(argc, argv); 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"); FILE *fp = fopen(argv[i], "rb");
if (fp == NULL) if (fp == NULL)
die(argv[i]); die(argv[i]);
@ -91,10 +91,14 @@ int main(int argc, char *argv[]) {
run(fp, argv[i], tty); run(fp, argv[i], tty);
fclose(fp); fclose(fp);
if (i + 1 != argc) { if (tty && (i + 1 != argc)) {
fprintf(stderr, "\r\n"); // separate concurrent files fprintf(stderr, "\r\n"); // separate concurrent files in tty
} }
} }
if (offset == argc) {
run(stdin, "stdin", 1);
}
} else { } else {
run(stdin, "stdin", 1); // for piped-input or repl-like behavior run(stdin, "stdin", 1); // for piped-input or repl-like behavior
} }