- --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
#include <stdbool.h>
#define LAT_VERSION "0.4.0"
struct config {
bool color;
bool lines;

View File

@ -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++;
}

View File

@ -24,8 +24,8 @@ void run(FILE *fp, char *filename, int tty) {
if (tty) {
char *addon = f.binary ? "<binary>" : "";
fprintf(stderr, "\r%s%s%s%s\r\n", invert_t, basename(filename), addon,
uinvert_t);
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
}