A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
command-line-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Lawrence Livermore National Laboratory
4  *
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;
8  *
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.
13  *
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
17  *
18  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
19  */
20 
21 #include <iostream>
22 #include <iomanip>
23 #include <string>
24 
25 #include "ns3/core-module.h"
26 
27 
28 using namespace ns3;
29 
30 
31 std::string g_cbArg = "cbArg default";
32 
33 bool SetCbArg (std::string val)
34 {
35  g_cbArg = val;
36  return true;
37 }
38 
39 
40 int main (int argc, char *argv[])
41 {
42 
43  int intArg = 1;
44  bool boolArg = false;
45  std::string strArg = "strArg default";
46  // Attribute path
47  const std::string attrClass = "ns3::RandomVariableStream";
48  const std::string attrName = "Antithetic";
49  const std::string attrPath = attrClass + "::" + attrName;
50 
51  // Cache the initial values. Normally one wouldn't do this,
52  // but we want to demonstrate that CommandLine has changed them.
53  const int intDef = intArg;
54  const bool boolDef = boolArg;
55  const std::string strDef = strArg;
56  const std::string cbDef = g_cbArg;
57  // Look up default value for attribute
58  const TypeId tid = TypeId::LookupByName (attrClass);
59  std::string attrDef;
60  {
61  struct TypeId::AttributeInformation info;
62  tid.LookupAttributeByName (attrName, &info);
63  attrDef = info.originalInitialValue->SerializeToString (info.checker);
64  }
65 
66 
67  CommandLine cmd;
68  cmd.Usage ("CommandLine example program.\n"
69  "\n"
70  "This little program demonstrates how to use CommandLine.");
71  cmd.AddValue ("intArg", "an int argument", intArg);
72  cmd.AddValue ("boolArg", "a bool argument", boolArg);
73  cmd.AddValue ("strArg", "a string argument", strArg);
74  cmd.AddValue ("anti", attrPath);
75  cmd.AddValue ("cbArg", "a string via callback", MakeCallback (SetCbArg));
76  cmd.Parse (argc, argv);
77 
78  // Show initial values:
79  std::cout << std::endl;
80  std::cout << cmd.GetName () << ":" << std::endl;
81  std::cout << "Initial values:" << std::endl;
82 
83  std::cout << std::left << std::setw (10) << "intArg:"
84  << intDef
85  << std::endl;
86  std::cout << std::setw (10) << "boolArg:"
87  << std::boolalpha << boolDef << std::noboolalpha
88  << std::endl;
89 
90  std::cout << std::setw (10) << "strArg:"
91  << "\"" << strDef << "\""
92  << std::endl;
93  std::cout << std::setw (10) << "anti:"
94  << "\"" << attrDef << "\""
95  << std::endl;
96  std::cout << std::setw (10) << "cbArg:"
97  << "\"" << cbDef << "\""
98  << std::endl;
99  std::cout << std::endl;
100 
101 
102  // Show final values
103  std::cout << "Final values:" << std::endl;
104  std::cout << std::left << std::setw (10) << "intArg:"
105  << intArg
106  << std::endl;
107  std::cout << std::setw (10) << "boolArg:"
108  << std::boolalpha << boolArg
109  << std::noboolalpha
110  << std::endl;
111 
112  std::cout << std::setw (10) << "strArg:"
113  << "\"" << strArg << "\""
114  << std::endl;
115 
116  // Look up new default value for attribute
117  {
118  struct TypeId::AttributeInformation info;
119  tid.LookupAttributeByName (attrName, &info);
120 
121  std::cout << std::setw (10) << "anti:"
122  << "\""
123  << info.initialValue->SerializeToString (info.checker)
124  << "\""
125  << std::endl;
126  }
127  std::cout << std::setw (10) << "cbArg:"
128  << "\"" << g_cbArg << "\""
129  << std::endl;
130 
131  return 0;
132 }
Ptr< const AttributeValue > originalInitialValue
Definition: type-id.h:65
int main(int argc, char *argv[])
std::string GetName() const
Get the program name.
Definition: command-line.cc:95
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:89
Ptr< const AttributeValue > initialValue
Definition: type-id.h:66
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1283
Parse command-line arguments.
Definition: command-line.h:196
Ptr< const AttributeChecker > checker
Definition: type-id.h:68
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Definition: type-id.cc:588
bool SetCbArg(std::string val)
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:471
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string g_cbArg
a unique identifier for an interface.
Definition: type-id.h:49
static TypeId LookupByName(std::string name)
Definition: type-id.cc:535