-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_options.c
67 lines (57 loc) · 1.35 KB
/
handle_options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
// If VERSION is not defined, use a default value
#ifndef VERSION
#define VERSION "unknown"
#endif
void
print_version ()
{
printf ("%s\n", VERSION);
}
void
print_help ()
{
printf ("Usage: unpak [OPTIONS]\n");
printf ("\n");
printf ("Utility for unpacking any type of archive 📦\n");
printf ("\n");
printf ("Options:\n");
printf (" -v, --version Print the version information and exit.\n");
printf (" -h, --help Print this help message and exit.\n");
printf ("\n");
printf ("Usage example:\n");
printf (" unpak archive.7z\n");
}
void
handle_options (int argc, char *argv[])
{
/* Current option being processed. */
int opt;
/* Define the structure for long options. */
struct option long_options[] = {
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
/* Parse the command-line options and will not handle them accordingly. */
while ((opt = getopt_long (argc, argv, "vh", long_options, NULL)) != -1)
{
switch (opt)
{
/* Print the --version information. */
case 'v':
print_version ();
exit (0);
/* Print the --help information. */
case 'h':
print_help ();
exit (0);
/* Handle unknown options, provide a help message. */
default:
printf ("Use unpak --help\n");
exit (1);
}
}
}