1 /*
2  *                           OptList Usage Sample
3  *
4  *   File    : sample.c
5  *   Purpose : Demonstrates usage of optlist library.
6  *   Author  : Michael Dipperstein
7  *   Date    : July 23, 2004
8  *
9  ****************************************************************************
10  *
11  * Sample: A optlist library sample usage program
12  * Copyright (C) 2007, 2014 by
13  * Michael Dipperstein (mdipperstein@gmail.com)
14  *
15  * This file is part of the optlist library.
16  *
17  * The optlist library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public License as
19  * published by the Free Software Foundation; either version 3 of the
20  * License, or (at your option) any later version.
21  *
22  * The optlist library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
25  * General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public License
28  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29  *
30  */
31 module optlist_example.app;
32 
33 
34 private static import core.memory;
35 private static import core.stdc.stdio;
36 private static import core.stdc.stdlib;
37 private static import optlist_d;
38 
39 nothrow @nogc
40 void print_help(char** argv)
41 
42 	do
43 	{
44 		core.stdc.stdio.printf("Usage: %s <options>\n\n", optlist_d.FindFileName(argv[0]));
45 		core.stdc.stdio.printf("options:\n");
46 		core.stdc.stdio.printf("  -a : option excepting argument.\n");
47 		core.stdc.stdio.printf("  -b : option without arguments.\n");
48 		core.stdc.stdio.printf("  -c : option without arguments.\n");
49 		core.stdc.stdio.printf("  -d : option excepting argument.\n");
50 		core.stdc.stdio.printf("  -e : option without arguments.\n");
51 		core.stdc.stdio.printf("  -f : option without arguments.\n");
52 		core.stdc.stdio.printf("  -? : print out command line options.\n\n");
53 	}
54 
55 /**
56  * This is the main function for this program, it calls optlist to parse the command line input displays the results of the parsing.
57  *
58  * Params:
59  *      argc = number of parameters
60  *      argv = parameter list
61  *
62  * Effects: parses command line parameters
63  *
64  * Returns: EXIT_SUCCESS for success, otherwise EXIT_FAILURE.
65  */
66 extern (C)
67 nothrow @nogc @live
68 int main(int argc, char** argv)
69 
70 	do
71 	{
72 		/* get list of command line options and their arguments */
73 		optlist_d.option_t* optList = optlist_d.GetOptList(argc, argv, "a:bcd:ef?");
74 
75 		if (optList == null) {
76 			version (Windows) {
77 				/*
78 				 * Crash when using stderr on Windows.
79 				 * https://issues.dlang.org/show_bug.cgi?id=19933
80 				 */
81 				core.stdc.stdio.printf("parse error.\n");
82 			} else {
83 				core.stdc.stdio.fprintf(core.stdc.stdio.stderr, "parse error.\n");
84 			}
85 
86 			return core.stdc.stdlib.EXIT_FAILURE;
87 		}
88 
89 		/* display results of parsing */
90 		while (optList != null) {
91 			optlist_d.option_t* thisOpt = optList;
92 			optList = optList.next;
93 
94 			if (thisOpt.option == '?') {
95 				.print_help(argv);
96 
97 				/* free the rest of the list */
98 				optlist_d.FreeOptList(thisOpt);
99 
100 				return core.stdc.stdlib.EXIT_SUCCESS;
101 			}
102 
103 			core.stdc.stdio.printf("found option %c\n", thisOpt.option);
104 
105 			if (thisOpt.argument != null) {
106 				core.stdc.stdio.printf("\tfound argument %s", thisOpt.argument);
107 				core.stdc.stdio.printf(" at index %d\n", thisOpt.argIndex);
108 			} else {
109 				core.stdc.stdio.printf("\tno argument for this option\n");
110 			}
111 
112 			/* done with this item, free it */
113 			core.memory.pureFree(thisOpt);
114 		}
115 
116 		return core.stdc.stdlib.EXIT_SUCCESS;
117 	}