File: command_line.cpp

package info (click to toggle)
clanlib 1.0~svn3827-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,632 kB
  • ctags: 16,580
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (83 lines) | stat: -rw-r--r-- 1,890 bytes parent folder | download | duplicates (7)
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
77
78
79
80
81
82
83
#include <ClanLib/core.h>

class MyCommandLine
{
public:
	enum { ARG_ARG = 301 };

	CL_CommandLine argp;

	MyCommandLine(int argc, char** argv) 
	{
		argp.add_usage("[OPTIONS]... [FILES]...");
		argp.add_usage("foobar [OPTIONS]... [FILES]...");
	 
		argp.add_doc("This program does nothing usefull..");
	 
		argp.add_group ("Options that want a file:");
		argp.add_option('c',	  "config", "FILE", "Config the app");
		argp.add_option('f',	  "file",	"FILE", "Load a file");

		argp.add_group ("Options that don't want a file:");
		argp.add_option('z',	  "zero",	"",	  "Zero Args");
		argp.add_option('a',	  "",		 "",	  "short a");
		argp.add_option('h',	  "help",	"",	  "help");

		argp.add_group ("Options without a short arg:");
		argp.add_option(ARG_ARG, "arg",	 "",	  "long a");
	 
		argp.add_doc("For more info have a look at FOobar does nothing usefull..\n"
						 "Examples can be found at blubbark...\n"
						 "....");

		argp.parse_args(argc, argv);
	}
  
	void read_options() 
	{
		while (argp.next())
		{
			switch (argp.get_key()) 
			{
			case 'h':
				argp.print_help();
				break;
			case 'f':
				std::cout << "file: " << argp.get_argument() << std::endl;
				break;
			case 'c':
				std::cout << "config: " << argp.get_argument() << std::endl;
				break;
			case 'a':
				std::cout << "a" << std::endl;
				break;
			case 'z':
				std::cout << "zero" << std::endl;
				break;
			case ARG_ARG:
				std::cout << "arg" << std::endl;
				break;
			case CL_CommandLine::REST_ARG:
				std::cout << "rest: " << argp.get_argument() << std::endl;
				break;
			default:
				std::cout << "Got " << argp.get_key() << " " << argp.get_argument() << std::endl;
				break;
			}
		}
	}
};

int main(int argc, char** argv)
{
	try 
	{
		MyCommandLine args(argc, argv);
		args.read_options();
	} 
	catch (CL_Error& err) 
	{
		std::cout << argv[0] << err.message << std::endl;
	}
	return 0;
}