├── README ├── Makefile └── myprog.c /README: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -I/opt/local/include 3 | LDFLAGS = -L/opt/local/lib -largtable2 4 | 5 | -------------------------------------------------------------------------------- /myprog.c: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | Example source code for using the argtable2 library to implement: 3 | 4 | myprog [-lRv] [-k ] [-D MACRO]... [-o ] [--help] 5 | [--version] []... 6 | 7 | This file is part of the argtable2 library. 8 | Copyright (C) 1998-2001,2003-2010 Stewart Heitmann 9 | sheitmann@users.sourceforge.net 10 | 11 | The argtable2 library is free software; you can redistribute it and/or 12 | modify it under the terms of the GNU Library General Public License as 13 | published by the Free Software Foundation; either version 2 of the 14 | License, or (at your option) any later version. 15 | 16 | This software is distributed in the hope that it will be useful, 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | Library General Public License for more details. 20 | 21 | You should have received a copy of the GNU Library General Public 22 | License along with this library; if not, write to the Free Software 23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 24 | USA. 25 | **********************************************************************/ 26 | #include 27 | 28 | int 29 | mymain(int l, int R, int k, 30 | const char **defines, int ndefines, 31 | const char *outfile, 32 | int v, 33 | const char **infiles, int ninfiles) 34 | { 35 | int i; 36 | 37 | if (l > 0) 38 | printf("list files (-l)\n"); 39 | if (R > 0) 40 | printf("recurse through directories (-R)\n"); 41 | if (v > 0) 42 | printf("verbose is enabled (-v)\n"); 43 | printf("scalar k=%d\n", k); 44 | printf("output is \"%s\"\n", outfile); 45 | 46 | for (i = 0; i < ndefines; i++) 47 | printf("user defined macro \"%s\"\n", defines[i]); 48 | 49 | for (i = 0; i < ninfiles; i++) 50 | printf("infile[%d]=\"%s\"\n", i, infiles[i]); 51 | 52 | return 0; 53 | } 54 | 55 | 56 | int 57 | main(int argc, char **argv) 58 | { 59 | struct arg_lit *list = arg_lit0("lL", NULL, "list files"); 60 | struct arg_lit *recurse = arg_lit0("R", NULL, "recurse through subdirectories"); 61 | struct arg_int *repeat = arg_int0("k", "scalar", NULL, "define scalar value k (default is 3)"); 62 | struct arg_str *defines = arg_strn("D", "define", "MACRO", 0, argc + 2, "macro definitions"); 63 | struct arg_file *outfile = arg_file0("o", NULL, "", "output file (default is \"-\")"); 64 | struct arg_lit *verbose = arg_lit0("v", "verbose,debug", "verbose messages"); 65 | struct arg_lit *help = arg_lit0(NULL, "help", "print this help and exit"); 66 | struct arg_lit *version = arg_lit0(NULL, "version", "print version information and exit"); 67 | struct arg_file *infiles = arg_filen(NULL, NULL, NULL, 1, argc + 2, "input file(s)"); 68 | struct arg_end *end = arg_end(20); 69 | void *argtable[] = {list, recurse, repeat, defines, outfile, verbose, help, version, infiles, end}; 70 | const char *progname = "myprog"; 71 | int nerrors; 72 | int exitcode = 0; 73 | 74 | /* verify the argtable[] entries were allocated sucessfully */ 75 | if (arg_nullcheck(argtable) != 0) { 76 | /* 77 | * NULL entries were detected, some allocations must have 78 | * failed 79 | */ 80 | printf("%s: insufficient memory\n", progname); 81 | exitcode = 1; 82 | goto exit; 83 | } 84 | /* set any command line default values prior to parsing */ 85 | repeat->ival[0] = 3; 86 | outfile->filename[0] = "-"; 87 | 88 | /* Parse the command line as defined by argtable[] */ 89 | nerrors = arg_parse(argc, argv, argtable); 90 | 91 | /* special case: '--help' takes precedence over error reporting */ 92 | if (help->count > 0) { 93 | printf("Usage: %s", progname); 94 | arg_print_syntax(stdout, argtable, "\n"); 95 | printf("This program demonstrates the use of the argtable2 library\n"); 96 | printf("for parsing command line arguments. Argtable accepts integers\n"); 97 | printf("in decimal (123), hexadecimal (0xff), octal (0o123) and binary\n"); 98 | printf("(0b101101) formats. Suffixes KB, MB and GB are also accepted.\n"); 99 | arg_print_glossary(stdout, argtable, " %-25s %s\n"); 100 | exitcode = 0; 101 | goto exit; 102 | } 103 | /* special case: '--version' takes precedence error reporting */ 104 | if (version->count > 0) { 105 | printf("'%s' example program for the \"argtable\" command line argument parser.\n", progname); 106 | printf("September 2003, Stewart Heitmann\n"); 107 | exitcode = 0; 108 | goto exit; 109 | } 110 | /* If the parser returned any errors then display them and exit */ 111 | if (nerrors > 0) { 112 | /* Display the error details contained in the arg_end struct. */ 113 | arg_print_errors(stdout, end, progname); 114 | printf("Try '%s --help' for more information.\n", progname); 115 | exitcode = 1; 116 | goto exit; 117 | } 118 | /* 119 | * special case: uname with no command line options induces brief 120 | * help 121 | */ 122 | if (argc == 1) { 123 | printf("Try '%s --help' for more information.\n", progname); 124 | exitcode = 0; 125 | goto exit; 126 | } 127 | /* normal case: take the command line options at face value */ 128 | exitcode = mymain(list->count, recurse->count, repeat->ival[0], 129 | defines->sval, defines->count, 130 | outfile->filename[0], verbose->count, 131 | infiles->filename, infiles->count); 132 | 133 | exit: 134 | /* deallocate each non-null entry in argtable[] */ 135 | arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0])); 136 | 137 | return exitcode; 138 | } 139 | --------------------------------------------------------------------------------