1
/*********************************************************************
2
Example source code for using the argtable2 library to implement:
4
Usage: hasoptvalue -f <int> [-f <int>] [-f <int>] -b [<int>] [-b [<int>]] [-b [<int>]] [--help] [--version]
5
This program demonstrates the use of the argtable2 library
6
-f, --foo=<int> takes an integer value (defaults to 9)
7
-b, --bar=[<int>] takes an optional integer value (defaults to 5)
8
--help print this help and exit
9
--version print version information and exit
11
This file is part of the argtable2 library.
12
Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
13
sheitmann@users.sourceforge.net
15
The argtable2 library is free software; you can redistribute it and/or
16
modify it under the terms of the GNU Library General Public License as
17
published by the Free Software Foundation; either version 2 of the
18
License, or (at your option) any later version.
20
This software is distributed in the hope that it will be useful,
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
Library General Public License for more details.
25
You should have received a copy of the GNU Library General Public
26
License along with this library; if not, write to the Free Software
27
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
29
**********************************************************************/
30
#include <argtable2.h>
33
int main(int argc, char **argv)
35
struct arg_int *foo = arg_intn("f","foo", NULL,1,3, "takes an integer value (defaults to 9)");
36
struct arg_int *bar = arg_intn("b","bar", NULL,1,3, "takes an optional integer value (defaults to 5)");
37
struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
38
struct arg_lit *version = arg_lit0(NULL,"version", "print version information and exit");
39
struct arg_end *end = arg_end(20);
40
void* argtable[] = {foo,bar,help,version,end};
41
const char* progname = "hasoptvalue";
46
/* verify the argtable[] entries were allocated sucessfully */
47
if (arg_nullcheck(argtable) != 0)
49
/* NULL entries were detected, some allocations must have failed */
50
printf("%s: insufficient memory\n",progname);
55
/* set foo default values to 9 */
56
for (i=0; i<foo->hdr.maxcount; i++)
59
/* allow bar to have optional values */
60
/* and set bar default values to 5 */
61
bar->hdr.flag |= ARG_HASOPTVALUE;
62
for (i=0; i<bar->hdr.maxcount; i++)
65
/* Parse the command line as defined by argtable[] */
66
nerrors = arg_parse(argc,argv,argtable);
68
/* special case: '--help' takes precedence over error reporting */
71
printf("Usage: %s", progname);
72
arg_print_syntax(stdout,argtable,"\n");
73
printf("This program demonstrates the use of the argtable2 library\n");
74
arg_print_glossary(stdout,argtable," %-25s %s\n");
79
/* special case: '--version' takes precedence error reporting */
80
if (version->count > 0)
82
printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
83
printf("September 2005, Stewart Heitmann\n");
88
/* If the parser returned any errors then display them and exit */
91
/* Display the error details contained in the arg_end struct.*/
92
arg_print_errors(stdout,end,progname);
93
printf("Try '%s --help' for more information.\n",progname);
98
/* special case: uname with no command line options induces brief help */
101
printf("Try '%s --help' for more information.\n",progname);
106
/* command line arguments are successfully parsed at this point. */
107
/* print what we have parsed */
108
printf("%d instances of --foo detected on command line\n", foo->count);
109
for (i=0; i<foo->hdr.maxcount; i++)
110
printf("foo[%d] = %d\n", i, foo->ival[i]);
111
printf("%d instances of --bar detected on command line\n", bar->count);
112
for (i=0; i<bar->hdr.maxcount; i++)
113
printf("bar[%d] = %d\n", i, bar->ival[i]);
116
/* deallocate each non-null entry in argtable[] */
117
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));