Added basic argument parsing and better Makefile

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2024-10-07 21:39:00 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2024-10-07 21:39:00 +0200
Commit 4e2f5ec4bf2aec43f184674cfe9831d20e13e34a (patch)
-rw-r--r-- Makefile 6
-rw-r--r-- main.c 43
2 files changed, 47 insertions, 2 deletions
diff --git a/Makefile b/Makefile
  
1
CC      := cc
  
2
CFLAGS  := -Wall -Wextra -Wshadow -Wunused -Wswitch-enum -Wpedantic -Wundef
  
3
LDFLAGS := -lm -ldl -lpthread -lasound
  
4
  
1
ttdaw: main.c
5
ttdaw: main.c
2
	$(CC) -Wall -o ttdaw main.c minisdl_audio.c -lm -ldl -lpthread -lasound -lm
6
	$(CC) $(CFLAGS) -o ttdaw main.c minisdl_audio.c $(LDFLAGS)
3
  
7
  
diff --git a/main.c b/main.c
1
#include <stdio.h>
1
#include <stdio.h>
  
2
#include <stdlib.h>
  
3
#include <stdarg.h>
  
4
#include <getopt.h>
  
5
  
  
6
void help(const char *argv0) {
  
7
	printf("Usage: %s [options]\n"
  
8
			"\nAvailable options:\n"
  
9
			"  -h,--help                  this help\n"
  
10
			"  -v,--version               show version\n"
  
11
			"  -p,--port=client:port      device port\n",
  
12
			argv0);
  
13
}
2
  
14
  
3
int main(int argc, char *argv[]) {
15
int main(int argc, char *argv[]) {
4
	fprintf(stdout, "Oh, hi Mark!\n");
16
	const char short_options[] = "hvlp";
  
17
	const struct option long_options[] = {
  
18
		{"help", 0, NULL, 'h'},
  
19
		{"version", 0, NULL, 'v'},
  
20
		{"list", 0, NULL, 'l'},
  
21
		{"port", 1, NULL, 'p'},
  
22
		{0},
  
23
	};
  
24
  
  
25
	int c;
  
26
	while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  
27
		switch (c) {
  
28
			case 'h':
  
29
				help(argv[0]);
  
30
				return 0;
  
31
			case 'v':
  
32
				fprintf(stdout, "Version\n");
  
33
				return 0;
  
34
			case 'l':
  
35
				fprintf(stdout, "List\n");
  
36
				return 0;
  
37
			case 'p':
  
38
				fprintf(stdout, "Port\n");
  
39
				return 0;
  
40
			default:
  
41
				fprintf(stdout, "No option provided\n");
  
42
				return 0;
  
43
		}
  
44
	}
  
45
  
5
	return 0;
46
	return 0;
6
}
47
}
7
  
48