~ubuntu-branches/ubuntu/feisty/argtable2/feisty

« back to all changes in this revision

Viewing changes to example/hasoptvalue.c

  • Committer: Bazaar Package Importer
  • Author(s): Shachar Shemesh
  • Date: 2006-11-14 16:13:36 UTC
  • mfrom: (2.1.1 edgy)
  • Revision ID: james.westby@ubuntu.com-20061114161336-k2z40o650zfi7pjx
Tags: 6-3
* Update maintainer's email address (now a Debian Developer)
* Update the policy version to 3.7.2 (no changes required)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************************
 
2
Example source code for using the argtable2 library to implement:
 
3
 
 
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
 
10
 
 
11
This file is part of the argtable2 library.
 
12
Copyright (C) 1998-2001,2003-2006 Stewart Heitmann
 
13
sheitmann@users.sourceforge.net
 
14
 
 
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.
 
19
 
 
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.
 
24
 
 
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,
 
28
USA.
 
29
**********************************************************************/
 
30
#include <argtable2.h>
 
31
 
 
32
 
 
33
int main(int argc, char **argv)
 
34
    {
 
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";
 
42
    int nerrors;
 
43
    int exitcode=0;
 
44
    int i;
 
45
 
 
46
    /* verify the argtable[] entries were allocated sucessfully */
 
47
    if (arg_nullcheck(argtable) != 0)
 
48
        {
 
49
        /* NULL entries were detected, some allocations must have failed */
 
50
        printf("%s: insufficient memory\n",progname);
 
51
        exitcode=1;
 
52
        goto exit;
 
53
        }
 
54
 
 
55
    /* set foo default values to 9 */
 
56
    for (i=0; i<foo->hdr.maxcount; i++)
 
57
        foo->ival[i]=9;
 
58
 
 
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++)
 
63
        bar->ival[i]=5;
 
64
 
 
65
    /* Parse the command line as defined by argtable[] */
 
66
    nerrors = arg_parse(argc,argv,argtable);
 
67
 
 
68
    /* special case: '--help' takes precedence over error reporting */
 
69
    if (help->count > 0)
 
70
        {
 
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");
 
75
        exitcode=0;
 
76
        goto exit;
 
77
        }
 
78
 
 
79
    /* special case: '--version' takes precedence error reporting */
 
80
    if (version->count > 0)
 
81
        {
 
82
        printf("'%s' example program for the \"argtable\" command line argument parser.\n",progname);
 
83
        printf("September 2005, Stewart Heitmann\n");
 
84
        exitcode=0;
 
85
        goto exit;
 
86
        }
 
87
 
 
88
    /* If the parser returned any errors then display them and exit */
 
89
    if (nerrors > 0)
 
90
        {
 
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);
 
94
        exitcode=1;
 
95
        goto exit;
 
96
        }
 
97
 
 
98
    /* special case: uname with no command line options induces brief help */
 
99
    if (argc==1)
 
100
        {
 
101
        printf("Try '%s --help' for more information.\n",progname);
 
102
        exitcode=0;
 
103
        goto exit;
 
104
        }
 
105
 
 
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]);         
 
114
 
 
115
    exit:
 
116
    /* deallocate each non-null entry in argtable[] */
 
117
    arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
 
118
 
 
119
    return exitcode;
 
120
    }