-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
76 lines (57 loc) · 1.81 KB
/
main.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
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "arghlp.h"
#define HEADER "This is just a demo ...\n\
to see how to use it"
#define FOOTER "\
This is simply the help footer like What you see in regular POSIX command line e.g:\n\
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>\n\
Full documentation <https://www.gnu.org/software/coreutils/cat>\n\
or available locally via: info '(coreutils) cat invocation'"
struct optionx optlx[] ={
{{"help", no_argument , nullable, 'h'} , "\t\tprint help of the program"},
{{"version",no_argument,nullable, 'v'} , "\t\tprint the version of the program"},
{{"input", required_argument,nullable, 'i'} , "<file>\tinput of the program"},
{{"output",optional_argument,nullable, 'o'} , "[file]\toutput of the program"},
OPTX_END
};
void * argparse(int ac ,char * const * av , const char *shortopt , struct option * opts , void * args)
{
int opt = 0 ;
if (1 == ac ) __help__ ;
while( (opt = getopt_long(ac , av, shortopt , opts , 0)) != ~0 )
{
switch(opt)
{
case 'h':
__help__;
break;
case 'v':
puts("version 1.x.x");
break;
case 'i':
char *input = optarg ;
fprintf(stdout, "input -> %s\n" , input) ;
break;
case 'o':
char *output = optentry( av , ac , optind) ;
fprintf(stdout, "output -> %s\n" , output) ;
break;
default:
fprintf(stderr , "no allowed ...\n");
break ;
}
}
return (void *) args ;
}
int main (int ac , char **av)
{
struct arghlp arghlp = {
.synopsys = {HEADER , FOOTER} ,
.options = optlx,
.ah_handler = argparse
} ;
arghlp_context(ac , av , &arghlp , nullable) ;
return EXIT_SUCCESS ;
}