TF++, Miscellaneous classes and modules in C++:

◆ main()

int main ( int  iargc,
char *  argv[] 
)

Program to test class tfxx::comdline::Commandline.

See also
tfxx::cmdline::Commandline
tfxx::cmdline
Commandline options and arguments.

Definition at line 54 of file commandlinetest.cc.

References tfxx::cmdline::arg_no, tfxx::cmdline::arg_opt, tfxx::cmdline::arg_yes, tfxx::cmdline::Commandline::bool_arg(), COMMANDLINETEST_VERSION, tfxx::cmdline::Commandline::double_arg(), tfxx::cmdline::Commandline::extra(), tfxx::cmdline::Commandline::float_arg(), tfxx::cmdline::Commandline::int_arg(), tfxx::cmdline::Commandline::long_arg(), tfxx::cmdline::Commandline::next(), tfxx::cmdline::Declare::opt_string, tfxx::cmdline::Commandline::optset(), tfxx::cmdline::parse_cmdline(), and tfxx::cmdline::Commandline::string_arg().

55 {
56 
57  // define usage information
58  char usage_text[]=
59  {
61  "usage: commandlinetest argument [-v] [-o n] [-f name] [-q [r]]" "\n"
62  " [-longopt f] [-F] [value [value ...]]" "\n"
63  " or: commandlinetest --help|-h" "\n"
64  };
65 
66  // define full help text
67  char help_text[]=
68  {
69  "argument one argument is mandatory" "\n"
70  "value additional arguments (optional)" "\n"
71  " " "\n"
72  "options:" "\n"
73  "-v set a flag (no argument is allowed)" "\n"
74  "-o n set an integer value (argument mandatory)" "\n"
75  "-f name set a string value (argument mandatory)" "\n"
76  "-q r set an integer value (argument optional)" "\n"
77  "-longopt f set the value for a long option name" "\n"
78  "-F evaluate file name options" "\n"
79  " availabe keys are: \"f\", \"dl\", \"k\"" "\n"
80  " " "\n"
81  "Long options may be abbreviated - test it!" "\n"
82  " " "\n"
83  "Notice that you have to use the syntax" "\n"
84  " " "\n"
85  " -q=argument" "\n"
86  " " "\n"
87  "with the optional argument for option -q. This syntax" "\n"
88  "is mandatory with optional arguments, but may also be used" "\n"
89  "with mandatory arguments." "\n"
90  " " "\n"
91  COMMANDLINETEST_CVSID
92  };
93 
94  // define commandline options
95  using namespace tfxx::cmdline;
96  static Declare options[]=
97  {
98  // 0: print help
99  {"help",arg_no,"-"},
100  // 1: verbose mode
101  {"v",arg_no,"-"},
102  // 2: verbose mode
103  {"o",arg_yes,"45"},
104  // 3: verbose mode
105  {"f",arg_yes,"a_string"},
106  // 4: verbose mode
107  {"q",arg_opt,"10.5"},
108  // 5: verbose mode
109  {"longopt",arg_yes,"default"},
110  // 6: evaluate filename options
111  {"F",arg_no,"-"},
112  {NULL}
113  };
114 
115  // define filename options
116  static const char* keys[]={"f", "dl", "k", 0};
117 
118  // no arguments? print usage...
119  if (iargc<2)
120  {
121  cerr << usage_text << endl;
122  exit(0);
123  }
124 
125  // collect options from commandline
126  tfxx::cmdline::Commandline cmdline(iargc, argv, options);
127 
128  // help requested? print full help text...
129  if (cmdline.optset(0))
130  {
131  cerr << usage_text << endl;
132  cerr << help_text << endl;
133  exit(0);
134  }
135 
136  cout << "Here I tell you about the options you supplied:" << endl;
137  // dummy operation: print option settings
138  for (int iopt=0; iopt<6; iopt++)
139  {
140  cout << endl;
141  cout << "option: '" << options[iopt].opt_string << "'" << endl;
142  if (cmdline.optset(iopt)) { cout << " option was set"; }
143  else { cout << " option was not set"; }
144  cout << endl;
145  cout << " argument (string): '"
146  << cmdline.string_arg(iopt) << "'" << endl;
147  cout << " argument (int): '"
148  << cmdline.int_arg(iopt) << "'" << endl;
149  cout << " argument (long): '"
150  << cmdline.long_arg(iopt) << "'" << endl;
151  cout << " argument (float): '"
152  << cmdline.float_arg(iopt) << "'" << endl;
153  cout << " argument (double): '"
154  << cmdline.double_arg(iopt) << "'" << endl;
155  cout << " argument (bool): '";
156  if (cmdline.bool_arg(iopt))
157  { cout << "true"; } else { cout << "false"; }
158  cout << "'" << endl;
159  }
160 
161  // print options in the expected way
162  cout << endl;
163  cout << "evaluating your choice:" << endl;
164  cout << " verbose mode switched ";
165  if (cmdline.optset(1)) { cout << "ON"; } else { cout << "OFF"; }
166  cout << endl;
167  if (cmdline.optset(2)) { cout << " set:"; }
168  else { cout << " default:"; }
169  cout << " -o: '" << cmdline.int_arg(2) << "'" << endl;
170  if (cmdline.optset(3)) { cout << " set:"; }
171  else { cout << " default:"; }
172  cout << " -f: '" << cmdline.string_arg(3) << "'" << endl;
173  if (cmdline.optset(4)) { cout << " set:"; }
174  else { cout << " default:"; }
175  cout << " -q: '" << cmdline.float_arg(4) << "'" << endl;
176  if (cmdline.optset(5)) { cout << " set:"; }
177  else { cout << " default:"; }
178  cout << " -longopt: '" << cmdline.string_arg(5) << "'" << endl;
179 
180  if (!cmdline.optset(6))
181  {
182  // dummy operation: print rest of command line
183  cout << endl;
184  cout << "And here is the rest of your command line arguments:" << endl;
185  while (cmdline.extra()) { cout << cmdline.next() << endl; }
186  }
187  else
188  {
189  cout << endl;
190  cout << "Parse file names and options" << endl;
191  cout << "Supported options keys are:" << endl;
192  const char** thekeys=keys;
193  const char* key=*thekeys;
194  while (key != NULL)
195  {
196  std::cout << " next key is: \"" << key << "\"" << std::endl;
197  ++thekeys;
198  key=*thekeys;
199  }
200  Tparsed result=parse_cmdline(cmdline, keys, cmdline.optset(1));
201  Tparsed::const_iterator i=result.begin();
202  while(i!=result.end())
203  {
204  std::cout << "filename: \"" << i->name << "\"" << std::endl;
205  Toptionmap::const_iterator j=i->options.begin();
206  while(j!=i->options.end())
207  {
208  std::cout << " " << j->first
209  << " \"" << j->second << "\"" << std::endl;
210  ++j;
211  }
212  const char ** k=keys;
213  while (*k != NULL)
214  {
215  if (i->haskey(*k))
216  {
217  std::cout << " key " << *k << ": found " << i->count(*k)
218  << " entries" << std::endl;
219  std::cout << " first entry is \"" << i->value(*k) <<
220  "\"" << std::endl;
221  Toptionmap e=i->extract(*k);
222  std::cout << " all entries are:" << std::endl;
223  Toptionmap::const_iterator p=e.begin();
224  while(p != e.end())
225  {
226  std::cout << " \"" << p->second << "\"" << std::endl;
227  ++p;
228  }
229  }
230  ++k;
231  key=*thekeys;
232  }
233  // next filename
234  ++i;
235  }
236  }
237 }
const char * opt_string
option name
Definition: commandline.h:138
Tparsed parse_cmdline(tfxx::cmdline::Commandline &c, const char **keys, const bool &debug)
parse command line arguments for file names and options ,A tutorial is available in the detailed desc...
Definition: xcmdline.cc:83
option requires an argument
Definition: commandline.h:102
std::list< Filename > Tparsed
list to hold file names with options ,A tutorial is available in the detailed description of the Inte...
Definition: xcmdline.h:218
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
option may have an optional argument
Definition: commandline.h:104
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
option has no argument
Definition: commandline.h:100
std::multimap< std::string, std::string > Toptionmap
map to hold file options ,A std::multimap is an STL container of pairs. It provides STL iterators to ...
Definition: xcmdline.h:145
#define COMMANDLINETEST_VERSION
Here is the call graph for this function: