66 for (Items::const_iterator i = cmd.
m_items.begin ();
79 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
113 for (argc--, argv++; argc > 0; argc--, argv++)
116 std::string param = *argv;
117 std::string::size_type cur = param.find (
"--");
120 param = param.substr (2, param.size () - 2);
124 cur = param.find (
"-");
127 param = param.substr (1, param.size () - 1);
135 cur = param.find (
"=");
136 std::string name, value;
137 if (cur == std::string::npos)
144 name = param.substr (0, cur);
145 value = param.substr (cur + 1, param.size () - (cur+1));
156 os <<
m_name <<
" [Program Arguments] [General Arguments]"
168 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
170 width = std::max (width, (*i)->m_name.size ());
175 os <<
"Program Arguments:" << std::endl;
176 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
179 << std::left << std::setw (width) << ( (*i)->m_name +
":")
183 if ( (*i)->HasDefault ())
185 os <<
" [" << (*i)->GetDefault () <<
"]";
193 <<
"General Arguments:\n"
194 <<
" --PrintGlobals: Print the list of globals.\n"
195 <<
" --PrintGroups: Print the list of groups.\n"
196 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
197 <<
" --PrintTypeIds: Print all TypeIds.\n"
198 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
199 <<
" --PrintHelp: Print this help message.\n"
208 os <<
"Global values:" << std::endl;
211 std::vector<std::string> globals;
217 std::stringstream ss;
218 ss <<
" --" << (*i)->GetName () <<
"=[";
222 ss << v.
Get () <<
"]" << std::endl;
223 ss <<
" " << (*i)->GetHelp () << std::endl;
224 globals.push_back (ss.str ());
226 std::sort (globals.begin (), globals.end ());
227 for (std::vector<std::string>::const_iterator it = globals.begin ();
243 NS_FATAL_ERROR (
"Unknown type=" << type <<
" in --PrintAttributes");
246 os <<
"Attributes for TypeId " << tid.
GetName () << std::endl;
249 std::vector<std::string> attributes;
253 std::stringstream ss;
258 ss <<
" " << info.
help << std::endl;
259 attributes.push_back (ss.str ());
261 std::sort (attributes.begin (), attributes.end ());
262 for (std::vector<std::string>::const_iterator it = attributes.begin ();
263 it < attributes.end ();
276 os <<
"TypeIds in group " << group <<
":" << std::endl;
279 std::vector<std::string> groupTypes;
283 std::stringstream ss;
287 ss <<
" " <<tid.
GetName () << std::endl;
289 groupTypes.push_back (ss.str ());
291 std::sort (groupTypes.begin (), groupTypes.end ());
292 for (std::vector<std::string>::const_iterator it = groupTypes.begin ();
293 it < groupTypes.end ();
304 os <<
"Registered TypeIds:" << std::endl;
307 std::vector<std::string> types;
311 std::stringstream ss;
313 ss <<
" " << tid.
GetName () << std::endl;
314 types.push_back (ss.str ());
316 std::sort (types.begin (), types.end ());
317 for (std::vector<std::string>::const_iterator it = types.begin ();
330 std::set<std::string> groups;
337 os <<
"Registered TypeId groups:" << std::endl;
339 for (std::set<std::string>::const_iterator k = groups.begin ();
343 os <<
" " << *k << std::endl;
352 NS_LOG_DEBUG (
"Handle arg name=" << name <<
" value=" << value);
353 if (name ==
"PrintHelp" || name ==
"help")
359 else if (name ==
"PrintGroups")
365 else if (name ==
"PrintTypeIds")
371 else if (name ==
"PrintGlobals")
377 else if (name ==
"PrintGroup")
383 else if (name ==
"PrintAttributes")
391 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
393 if ((*i)->m_name == name)
395 if (!(*i)->Parse (value))
397 std::cerr <<
"Invalid argument value: "
398 << name <<
"=" << value << std::endl;
411 std::cerr <<
"Invalid command-line arguments: --"
412 << name <<
"=" << value << std::endl;
422 NS_LOG_DEBUG (
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
423 return m_callback (value);
428 const std::string &help,
441 const std::string &attributePath)
445 size_t colon = attributePath.rfind (
"::");
446 const std::string typeName = attributePath.substr (0, colon);
447 NS_LOG_DEBUG (
"typeName: '" << typeName <<
"', colon: " << colon);
455 const std::string attrName = attributePath.substr (colon + 2);
462 std::stringstream ss;
464 <<
" (" << attributePath <<
") ["
475 const std::string value)
503 std::ostringstream oss;
504 oss << std::boolalpha << val;
512 std::string src = value;
513 std::transform(src.begin(), src.end(), src.begin(), ::tolower);
515 if (src.length () == 0)
520 else if ( (src ==
"true") || (src ==
"t") )
525 else if ( (src ==
"false") || (src ==
"f"))
532 std::istringstream iss;
535 return !iss.bad () && !iss.fail ();
~CommandLine()
Destructor.
uint32_t GetAttributeN(void) const
smart pointer class similar to boost::intrusive_ptr
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
std::string Get(void) const
Items m_items
The list of arguments.
hold variables of type string
void PrintGlobals(std::ostream &os) const
Handler for --PrintGlobals: print all global variables and values.
Callback< R > MakeBoundCallback(R(*fnPtr)(TX), ARG a1)
Make Callbacks with one bound argument.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Vector::const_iterator Iterator
#define NS_FATAL_ERROR(msg)
fatal error handling
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
std::string GetDefault< bool >(const bool &val)
Helper to specialize UserItem::GetDefault on bool.
std::string GetName() const
Get the program name.
void Usage(const std::string usage)
Supply the program usage and documentation.
bool SetDefaultFailSafe(std::string fullName, const AttributeValue &value)
void Clear(void)
Remove all arguments, Usage(), name.
void PrintAttributes(std::ostream &os, const std::string &type) const
Handler for --PrintAttributes: print the attributes for a given type.
An argument Item using a Callback to parse the input.
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
static bool HandleAttribute(const std::string name, const std::string value)
Callback function to handle attributes.
std::string m_usage
The Usage string.
static uint32_t GetRegisteredN(void)
virtual std::string GetDefault() const
static Iterator Begin(void)
static TypeId GetRegistered(uint32_t i)
std::string m_name
The program name.
Parse command-line arguments.
virtual bool Parse(const std::string value)
Parse from a string.
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
CommandLine & operator=(const CommandLine &cmd)
Assignment.
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
std::string m_name
Argument label: ---m_name=...
bool UserItemParse< bool >(const std::string value, bool &val)
Helper to specialize UserItem::Parse on bool.
Callback< bool, std::string > m_callback
The Callback.
std::string GetName(void) const
virtual bool HasDefault() const
virtual ~Item()
Destructor.
bool SetGlobalFailSafe(std::string name, const AttributeValue &value)
std::string m_help
Argument help string.
CommandLine()
Constructor.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
static Iterator End(void)
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
void PrintTypeIds(std::ostream &os) const
Handler for --PrintTypeIds: print all TypeId names.
void Copy(const CommandLine &cmd)
Copy constructor.
void PrintGroup(std::ostream &os, const std::string &group) const
Handler for --PrintGroup: print all types belonging to a given group.
std::string GetAttributeFullName(uint32_t i) const
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string GetGroupName(void) const
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
std::list< std::string > Split(std::string path)
a unique identifier for an interface.
void HandleArgument(const std::string &name, const std::string &value) const
Match name against the program or general arguments, and dispatch to the appropriate handler...