1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3 * Copyright (c) 2008 INRIA
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20 #ifndef COMMAND_LINE_H
21 #define COMMAND_LINE_H
27 #include "ns3/callback.h"
32 * \brief parse command-line arguments
35 * Instances of this class can be used to parse command-line
36 * arguments: users can register new arguments with
37 * CommandLine::AddValue but the most important functionality
38 * provided by this class is that it can be used to set the
39 * 'initial value' of every attribute in the system with the
40 * --TypeIdName::AttributeName=value syntax and it can be used
41 * to set the value of every GlobalValue in the system with
42 * the --GlobalValueName=value syntax.
50 * \param name the name of the user-supplied argument
51 * \param help some help text used by --PrintHelp
52 * \param value a reference to the variable where the
53 * value parsed will be stored (if no value
54 * is parsed, this variable is not modified).
57 void AddValue (const std::string &name,
58 const std::string &help,
63 * \param name the name of the user-supplied argument
64 * \param help some help text used by --PrintHelp
65 * \param callback a callback function that will be invoked to parse
66 * and collect the value. This normally used by language bindings.
68 void AddValue (const std::string &name,
69 const std::string &help,
70 Callback<bool, std::string> callback);
73 * \param argc the 'argc' variable: number of arguments (including the
74 * main program name as first element).
75 * \param argv the 'argv' variable: a null-terminated array of strings,
76 * each of which identifies a command-line argument.
78 * Obviously, this method will parse the input command-line arguments and
79 * will attempt to handle them all.
81 void Parse (int argc, char *argv[]) const;
89 virtual bool Parse (std::string value) = 0;
92 class UserItem : public Item
95 virtual bool Parse (std::string value);
98 class CallbackItem : public Item
101 virtual bool Parse (std::string value);
102 Callback<bool, std::string> m_callback;
105 void HandleArgument (std::string name, std::string value) const;
106 void PrintHelp (void) const;
107 void PrintGlobals (void) const;
108 void PrintAttributes (std::string type) const;
109 void PrintGroup (std::string group) const;
110 void PrintTypeIds (void) const;
111 void PrintGroups (void) const;
113 typedef std::list<Item *> Items;
121 template <typename T>
123 CommandLine::AddValue (const std::string &name,
124 const std::string &help,
127 UserItem<T> *item = new UserItem<T> ();
130 item->m_valuePtr = &value;
131 m_items.push_back (item);
134 template <typename T>
136 CommandLine::UserItem<T>::Parse (std::string value)
138 std::istringstream iss;
140 iss >> (*m_valuePtr);
141 return !iss.bad () && !iss.fail ();
146 #endif /* COMMAND_LINE_H */