TF++, Miscellaneous classes and modules in C++:
xcmdline.cc
Go to the documentation of this file.
1 
34 #define TF_XCMDLINE_CC_VERSION \
35  "TF_XCMDLINE_CC V1.0 "
36 
37 #include <tfxx/xcmdline.h>
38 
39 namespace tfxx {
40  namespace cmdline {
41 
42  namespace helper {
43 
44  typedef std::list<std::string> Tkeylist;
45 
47  public:
48  CmdlineArgument(const std::string& arg,
49  const Tkeylist& keys);
50  bool isoption() const { return(Misoption); }
51  std::string value() const { return(Mvalue); }
52  Tfileoption option() const { return(Moption); }
53  private:
54  std::string Mvalue;
56  bool Misoption;
57  }; // class CmdlineArgument
58 
59  CmdlineArgument::CmdlineArgument(const std::string& arg,
60  const Tkeylist& keys)
61  {
62  Mvalue=arg;
63  Misoption=false;
64  helper::Tkeylist::const_iterator i=keys.begin();
65  // std::cout << Mvalue << std::endl;
66  while ((i!=keys.end()) && (!Misoption))
67  {
68  // std::cout << *i << " " << Mvalue.find(*i) << std::endl;
69  if (Mvalue.find(*i) == 0)
70  {
71  Misoption=true;
72  Moption.first = i->substr(0, i->size()-1);
73  Moption.second= Mvalue.substr(Moption.first.size()+1);
74  }
75  ++i;
76  }
77  } // CmdlineArgument constructor
78 
79  } // namespace helper
80 
81  /*----------------------------------------------------------------------*/
82 
84  const char** keys,
85  const bool& debug)
86  {
87  // fill a search list
88  // ------------------
89  helper::Tkeylist keylist;
90  const char* key=*keys;
91  while (key != NULL)
92  {
93  std::string keystring(key);
94  keylist.push_back(keystring+":");
95  ++keys;
96  key=*keys;
97  }
98  // report search list if requested
99  if (debug)
100  {
101  std::cout << "DEBUG (parse_cmdline): keys are ";
102  helper::Tkeylist::const_iterator i=keylist.begin();
103  while (i!=keylist.end())
104  {
105  std::cout << "\"" << *i << "\" ";
106  ++i;
107  }
108  std::cout << std::endl;
109  }
110 
111  // parse rest of command line
112  // --------------------------
113  Tparsed retval;
114  Filename filename;
115  bool first=true;
116  while (c.extra())
117  {
118  helper::CmdlineArgument ca(c.next(), keylist);
119  if (first)
120  {
121  first=false;
122  filename.name=ca.value();
123  }
124  else
125  {
126  if (ca.isoption())
127  {
128  filename.options.insert(ca.option());
129  }
130  else
131  {
132  retval.push_back(filename);
133  filename.options.clear();
134  filename.name=ca.value();
135  }
136  }
137  }
138  if (!first) { retval.push_back(filename); }
139 
140  return(retval);
141  } // parse_cmdline
142 
143  /*======================================================================*/
144 
145  Toptionmap Filename::extract(const std::string& key) const
146  {
147  typedef Toptionmap::const_iterator Tomi;
148  typedef std::pair<Tomi, Tomi> Tomipair;
149  Tomipair equalrange=options.equal_range(key);
150  Toptionmap retval(equalrange.first, equalrange.second);
151  return retval;
152  }
153 
154  /*----------------------------------------------------------------------*/
155 
156  std::string Filename::value(const std::string& key) const
157  {
158  std::string retval;
159  Toptionmap::const_iterator p=options.find(key);
160  if (p != options.end())
161  { retval=p->second; }
162  return retval;
163  } // Filename::value
164 
165  } // namespace cmdline
166 
167 } // namespace tfxx
168 
169 /* ----- END OF xcmdline.cc ----- */
Toptionmap options
multimap containing all arguments together with their keys
Definition: xcmdline.h:196
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
std::pair< std::string, std::string > Tfileoption
pair to hold file option ,A std::pair is an STL container. In this it contains a key together with th...
Definition: xcmdline.h:130
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
std::list< std::string > Tkeylist
Definition: xcmdline.cc:44
char * next()
returns char-array of next commandline argument
Definition: commandline.h:243
A struct to hold filename together with options ,If a filename is given together with its specific pa...
Definition: xcmdline.h:192
bool extra() const
true if there are more commandline arguments
Definition: commandline.h:240
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
Toptionmap extract(const std::string &key) const
return all entries for a given key
Definition: xcmdline.cc:145
CmdlineArgument(const std::string &arg, const Tkeylist &keys)
Definition: xcmdline.cc:59
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
std::string name
filename
Definition: xcmdline.h:194
Namespace containing all code of library libtfxx.
std::string value(const std::string &key) const
return values for option with given key
Definition: xcmdline.cc:156