A Discrete-Event Network Simulator
API
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 
33 using namespace ns3;
34 
35 
40 std::string g_cbArg = "cbArg default";
41 
49 bool SetCbArg (std::string val)
50 {
51  g_cbArg = val;
52  return true;
53 }
54 
55 
56 int main (int argc, char *argv[])
57 {
58 
59  int intArg = 1;
60  bool boolArg = false;
61  std::string strArg = "strArg default";
62  // Attribute path
63  const std::string attrClass = "ns3::RandomVariableStream";
64  const std::string attrName = "Antithetic";
65  const std::string attrPath = attrClass + "::" + attrName;
66 
67  // Cache the initial values. Normally one wouldn't do this,
68  // but we want to demonstrate that CommandLine has changed them.
69  const int intDef = intArg;
70  const bool boolDef = boolArg;
71  const std::string strDef = strArg;
72  const std::string cbDef = g_cbArg;
73  // Look up default value for attribute
74  const TypeId tid = TypeId::LookupByName (attrClass);
75  std::string attrDef;
76  {
77  struct TypeId::AttributeInformation info;
78  tid.LookupAttributeByName (attrName, &info);
79  attrDef = info.originalInitialValue->SerializeToString (info.checker);
80  }
81 
82 
84  cmd.Usage ("CommandLine example program.\n"
85  "\n"
86  "This little program demonstrates how to use CommandLine.");
87  cmd.AddValue ("intArg", "an int argument", intArg);
88  cmd.AddValue ("boolArg", "a bool argument", boolArg);
89  cmd.AddValue ("strArg", "a string argument", strArg);
90  cmd.AddValue ("anti", attrPath);
91  cmd.AddValue ("cbArg", "a string via callback", MakeCallback (SetCbArg));
92  cmd.Parse (argc, argv);
93 
94  // Show initial values:
95  std::cout << std::endl;
96  std::cout << cmd.GetName () << ":" << std::endl;
97  std::cout << "Initial values:" << std::endl;
98 
99  std::cout << std::left << std::setw (10) << "intArg:"
100  << intDef
101  << std::endl;
102  std::cout << std::setw (10) << "boolArg:"
103  << std::boolalpha << boolDef << std::noboolalpha
104  << std::endl;
105 
106  std::cout << std::setw (10) << "strArg:"
107  << "\"" << strDef << "\""
108  << std::endl;
109  std::cout << std::setw (10) << "anti:"
110  << "\"" << attrDef << "\""
111  << std::endl;
112  std::cout << std::setw (10) << "cbArg:"
113  << "\"" << cbDef << "\""
114  << std::endl;
115  std::cout << std::endl;
116 
117 
118  // Show final values
119  std::cout << "Final values:" << std::endl;
120  std::cout << std::left << std::setw (10) << "intArg:"
121  << intArg
122  << std::endl;
123  std::cout << std::setw (10) << "boolArg:"
124  << std::boolalpha << boolArg
125  << std::noboolalpha
126  << std::endl;
127 
128  std::cout << std::setw (10) << "strArg:"
129  << "\"" << strArg << "\""
130  << std::endl;
131 
132  // Look up new default value for attribute
133  {
134  struct TypeId::AttributeInformation info;
135  tid.LookupAttributeByName (attrName, &info);
136 
137  std::cout << std::setw (10) << "anti:"
138  << "\""
139  << info.initialValue->SerializeToString (info.checker)
140  << "\""
141  << std::endl;
142  }
143  std::cout << std::setw (10) << "cbArg:"
144  << "\"" << g_cbArg << "\""
145  << std::endl;
146 
147  return 0;
148 }
tuple cmd
Definition: second.py:35
std::string GetName() const
Get the program name.
void Usage(const std::string usage)
Supply the program usage and documentation.
Definition: command-line.cc:96
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Attribute implementation.
Definition: type-id.h:76
Parse command-line arguments.
Definition: command-line.h:205
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:866
bool SetCbArg(std::string val)
Function to illustrate command line arguments handled by a Callback function.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
void Parse(int argc, char *argv[])
Parse the program arguments.
std::string g_cbArg
Global variable to illustrate command line arguments handled by a Callback function.
a unique identifier for an interface.
Definition: type-id.h:58
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:813