1 /* COPYING ******************************************************************
2 For copyright and licensing terms, see the file named COPYING.
3 // **************************************************************************
4 */
5 
6 #include <iostream>
7 #include <iomanip>
8 #include <cstring>
9 #include <cstdlib>
10 #include <cctype>
11 
12 #include "popt.h"
13 
14 using namespace popt;
15 
~table_definition()16 table_definition::~table_definition() {}
execute(processor & proc,char c)17 bool table_definition::execute(processor & proc, char c)
18 {
19 	for (unsigned i(0); i < count; ++i)
20 		if (array[i]->execute(proc, c))
21 			return true;
22 	return false;
23 }
execute(processor & proc,char c,const char * s)24 bool table_definition::execute(processor & proc, char c, const char * s)
25 {
26 	for (unsigned i(0); i < count; ++i)
27 		if (array[i]->execute(proc, c, s))
28 			return true;
29 	return false;
30 }
execute(processor & proc,const char * s)31 bool table_definition::execute(processor & proc, const char * s)
32 {
33 	for (unsigned i(0); i < count; ++i)
34 		if (array[i]->execute(proc, s))
35 			return true;
36 	return false;
37 }
help()38 void table_definition::help()
39 {
40 	for (unsigned i(0); i < count; ++i)
41 		if (dynamic_cast<named_definition *>(array[i])) {
42 			std::cout << description << ":\n";
43 			break;
44 		}
45 	std::size_t w = 0;
46 	for (unsigned i(0); i < count; ++i)
47 		if (named_definition * n = dynamic_cast<named_definition *>(array[i])) {
48 			std::size_t l = 0;
49 			if (n->query_short_name()) l += 2;
50 			if (const char * long_name = n->query_long_name()) {
51 				if (n->query_short_name()) l += 2;
52 				l += 2 + std::strlen(long_name);
53 			}
54 			if (const char * args_description = n->query_args_description())
55 				l += 1 + std::strlen(args_description);
56 			if (l > w) w = l;
57 		}
58 	for (unsigned i(0); i < count; ++i)
59 		if (named_definition * n = dynamic_cast<named_definition *>(array[i])) {
60 			std::string t;
61 			if (char short_name = n->query_short_name())
62 				t += "-" + std::string(1, short_name);
63 			if (const char * long_name = n->query_long_name()) {
64 				if (n->query_short_name()) t += ", ";
65 				t += "--" + std::string(long_name);
66 			}
67 			if (const char * args_description = n->query_args_description())
68 				t += " " + std::string(args_description);
69 			std::cout.put('\t') << std::setiosflags(std::iostream::left) << std::setw(static_cast<int>(w))
70 #if defined(__WATCOMC__) // Watcom C++ library bug: std::strings are not output as one lump
71 			<< t.c_str()
72 #else
73 			<< t
74 #endif
75 			;
76 			if (const char * entry_description = n->query_description())
77 				std::cout.put(' ') << entry_description;
78 			std::cout.put('\n');
79 		}
80 	for (unsigned i(0); i < count; ++i)
81 		if (table_definition * n = dynamic_cast<table_definition *>(array[i]))
82 			n->help();
83 }
long_usage()84 void table_definition::long_usage()
85 {
86 	for (unsigned i(0); i < count; ++i)
87 		if (named_definition * n = dynamic_cast<named_definition *>(array[i])) {
88 			const char * long_name = n->query_long_name();
89 			const char * args_description = n->query_args_description();
90 			if (long_name || args_description) {
91 				char short_name = n->query_short_name();
92 				std::cout << "[";
93 				if (args_description && short_name)
94 					std::cout.put('-').put(short_name);
95 				if (long_name) {
96 					if (args_description && short_name)
97 						std::cout.put('|');
98 					std::cout << "--" << long_name;
99 				}
100 				if (args_description)
101 					std::cout << " " << args_description;
102 				std::cout << "] ";
103 			}
104 		}
105 	for (unsigned i(0); i < count; ++i)
106 		if (table_definition * n = dynamic_cast<table_definition *>(array[i]))
107 			n->long_usage();
108 }
gather_combining_shorts(std::string & shorts)109 void table_definition::gather_combining_shorts(std::string & shorts)
110 {
111 	for (unsigned i(0); i < count; ++i)
112 		if (named_definition * n = dynamic_cast<named_definition *>(array[i])) {
113 			if (!n->query_args_description()) {
114 				if (char short_name = n->query_short_name())
115 					shorts += short_name;
116 			}
117 		}
118 	for (unsigned i(0); i < count; ++i)
119 		if (table_definition * n = dynamic_cast<table_definition *>(array[i]))
120 			n->gather_combining_shorts(shorts);
121 }
122