64 for (Items::const_iterator i = cmd.
m_items.begin ();
77 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
111 for (argc--, argv++; argc > 0; argc--, argv++)
114 std::string param = *argv;
115 std::string::size_type cur = param.find (
"--");
118 param = param.substr (2, param.size () - 2);
122 cur = param.find (
"-");
125 param = param.substr (1, param.size () - 1);
133 cur = param.find (
"=");
134 std::string name, value;
135 if (cur == std::string::npos)
142 name = param.substr (0, cur);
143 value = param.substr (cur + 1, param.size () - (cur+1));
154 os <<
m_name <<
" [Program Arguments] [General Arguments]"
166 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
168 width = std::max (width, (*i)->m_name.size ());
173 os <<
"Program Arguments:" << std::endl;
174 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
177 << std::left << std::setw (width) << ( (*i)->m_name +
":")
181 if ( (*i)->HasDefault ())
183 os <<
" [" << (*i)->GetDefault () <<
"]";
191 <<
"General Arguments:\n"
192 <<
" --PrintGlobals: Print the list of globals.\n"
193 <<
" --PrintGroups: Print the list of groups.\n"
194 <<
" --PrintGroup=[group]: Print all TypeIds of group.\n"
195 <<
" --PrintTypeIds: Print all TypeIds.\n"
196 <<
" --PrintAttributes=[typeid]: Print all attributes of typeid.\n"
197 <<
" --PrintHelp: Print this help message.\n"
206 os <<
"Global values:" << std::endl;
212 os <<
" --" << (*i)->GetName () <<
"=[";
216 os << v.
Get () <<
"]" << std::endl;
217 os <<
" " << (*i)->GetHelp () << std::endl;
229 NS_FATAL_ERROR (
"Unknown type=" << type <<
" in --PrintAttributes");
232 os <<
"Attributes for TypeId " << tid.
GetName () << std::endl;
240 os <<
" " << info.
help << std::endl;
250 os <<
"TypeIds in group " << group <<
":" << std::endl;
257 os <<
" " <<tid.
GetName () << std::endl;
266 os <<
"Registered TypeIds:" << std::endl;
270 os <<
" " << tid.
GetName () << std::endl;
279 std::list<std::string> groups;
289 for (std::list<std::string>::const_iterator j = groups.begin ();
301 groups.push_back (group);
305 os <<
"Registered TypeId groups:" << std::endl;
307 for (std::list<std::string>::const_iterator k = groups.begin ();
311 os <<
" " << *k << std::endl;
320 NS_LOG_DEBUG (
"Handle arg name=" << name <<
" value=" << value);
321 if (name ==
"PrintHelp" || name ==
"help")
327 else if (name ==
"PrintGroups")
333 else if (name ==
"PrintTypeIds")
339 else if (name ==
"PrintGlobals")
345 else if (name ==
"PrintGroup")
351 else if (name ==
"PrintAttributes")
359 for (Items::const_iterator i =
m_items.begin (); i !=
m_items.end (); ++i)
361 if ((*i)->m_name == name)
363 if (!(*i)->Parse (value))
365 std::cerr <<
"Invalid argument value: "
366 << name <<
"=" << value << std::endl;
379 std::cerr <<
"Invalid command-line arguments: --"
380 << name <<
"=" << value << std::endl;
390 NS_LOG_DEBUG (
"CommandLine::CallbackItem::Parse \"" << value <<
"\"");
391 return m_callback (value);
396 const std::string &help,
424 std::ostringstream oss;
425 oss << std::boolalpha << val;
433 std::string src = value;
434 std::transform(src.begin(), src.end(), src.begin(), ::tolower);
436 if (src.length () == 0)
441 else if ( (src ==
"true") || (src ==
"t") )
446 else if ( (src ==
"false") || (src ==
"f"))
453 std::istringstream iss;
456 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)
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.
Vector::const_iterator Iterator
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
std::string GetDefault< bool >(const bool &val)
Helper to specialize UserItem::GetDefault on bool.
#define NS_FATAL_ERROR(msg)
fatal error handling
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.
NS_LOG_COMPONENT_DEFINE("CommandLine")
An argument Item using a Callback to parse the input.
void PrintHelp(std::ostream &os) const
Print program usage to the desired output stream.
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.
void PrintGroups(std::ostream &os) const
Handler for --PrintGroups: print all TypeId group names.
CommandLine & operator=(const CommandLine &cmd)
Assignment.
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)
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...