CPlay/getopt/getopt_long.c
2025-11-14 20:59:40 +01:00

38 lines
1,004 B
C

#include <getopt.h>
#include <stdio.h>
#include <string.h>
#define VERSION "0.0.1"
int main(int argc, char *argv[]) {
int opt;
int option_index = 0;
static struct option long_opts[] = {
{"help", no_argument, 0, 'h'},
{"file", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0},
};
int has_file = 0;
char a[128];
while ((opt = getopt_long(argc, argv, "hvf:o:", long_opts, &option_index)) != -1) {
switch (opt) {
case 'h': puts("Help"); break;
case 'f':
printf("File: %s\n", optarg);
strncpy(a, optarg, 127);
has_file = 1;
break;
case 'o': printf("Output: %s\n", optarg); break;
case 'v': printf("Version: v%s\n", VERSION); break;
default: return 1;
}
}
if (has_file)
printf("Using file: %s\n", a);
return 0;
}