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 
34 using namespace ns3;
35 
36 namespace {
37 
42 std::string g_cbArg = "cbArg default";
43 
51 bool SetCbArg (std::string val)
52 {
53  g_cbArg = val;
54  return true;
55 }
56 
57 } // unnamed namespace
58 
59 
60 int main (int argc, char *argv[])
61 {
62 
63  int intArg = 1;
64  bool boolArg = false;
65  std::string strArg = "strArg default";
66  // Attribute path
67  const std::string attrClass = "ns3::RandomVariableStream";
68  const std::string attrName = "Antithetic";
69  const std::string attrPath = attrClass + "::" + attrName;
70  // Non-option arguments
71  int nonOpt1 = 1;
72  int nonOpt2 = 1;
73 
74  // Cache the initial values. Normally one wouldn't do this,
75  // but we want to demonstrate that CommandLine has changed them.
76  const int intDef = intArg;
77  const bool boolDef = boolArg;
78  const std::string strDef = strArg;
79  const std::string cbDef = g_cbArg;
80  // Look up default value for attribute
81  const TypeId tid = TypeId::LookupByName (attrClass);
82  std::string attrDef;
83  {
84  struct TypeId::AttributeInformation info;
85  tid.LookupAttributeByName (attrName, &info);
86  attrDef = info.originalInitialValue->SerializeToString (info.checker);
87  }
88  const int nonOpt1Def = nonOpt1;
89  const int nonOpt2Def = nonOpt2;
90 
91 
93  cmd.Usage ("CommandLine example program.\n"
94  "\n"
95  "This little program demonstrates how to use CommandLine.");
96  cmd.AddValue ("intArg", "an int argument", intArg);
97  cmd.AddValue ("boolArg", "a bool argument", boolArg);
98  cmd.AddValue ("strArg", "a string argument", strArg);
99  cmd.AddValue ("anti", attrPath);
100  cmd.AddValue ("cbArg", "a string via callback", MakeCallback (SetCbArg));
101  cmd.AddNonOption ("nonOpt1", "first non-option", nonOpt1);
102  cmd.AddNonOption ("nonOpt2", "first non-option", nonOpt2);
103  cmd.Parse (argc, argv);
104 
105  // Show initial values:
106  std::cout << std::endl;
107  std::cout << cmd.GetName () << ":" << std::endl;
108  std::cout << "Initial values:" << std::endl;
109 
110  std::cout << std::left << std::setw (10) << "intArg:"
111  << intDef
112  << std::endl;
113  std::cout << std::setw (10) << "boolArg:"
114  << std::boolalpha << boolDef << std::noboolalpha
115  << std::endl;
116 
117  std::cout << std::setw (10) << "strArg:"
118  << "\"" << strDef << "\""
119  << std::endl;
120  std::cout << std::setw (10) << "anti:"
121  << "\"" << attrDef << "\""
122  << std::endl;
123  std::cout << std::setw (10) << "cbArg:"
124  << "\"" << cbDef << "\""
125  << std::endl;
126  std::cout << std::left << std::setw (10) << "nonOpt1:"
127  << nonOpt1Def
128  << std::endl;
129  std::cout << std::left << std::setw (10) << "nonOpt2:"
130  << nonOpt2Def
131  << std::endl;
132  std::cout << std::endl;
133 
134 
135  // Show final values
136  std::cout << "Final values:" << std::endl;
137  std::cout << std::left << std::setw (10) << "intArg:"
138  << intArg
139  << std::endl;
140  std::cout << std::setw (10) << "boolArg:"
141  << std::boolalpha << boolArg << std::noboolalpha
142  << std::endl;
143 
144  std::cout << std::setw (10) << "strArg:"
145  << "\"" << strArg << "\""
146  << std::endl;
147 
148  // Look up new default value for attribute
149  {
150  struct TypeId::AttributeInformation info;
151  tid.LookupAttributeByName (attrName, &info);
152 
153  std::cout << std::setw (10) << "anti:"
154  << "\""
155  << info.initialValue->SerializeToString (info.checker)
156  << "\""
157  << std::endl;
158  }
159  std::cout << std::setw (10) << "cbArg:"
160  << "\"" << g_cbArg << "\""
161  << std::endl;
162  std::cout << std::left << std::setw (10) << "nonOpt1:"
163  << nonOpt1
164  << std::endl;
165  std::cout << std::left << std::setw (10) << "nonOpt2:"
166  << nonOpt2
167  << std::endl;
168  std::cout << std::left << "Number of extra non-option arguments:"
169  << cmd.GetNExtraNonOptions ()
170  << std::endl;
171 
172  for (std::size_t i = 0; i < cmd.GetNExtraNonOptions (); ++i)
173  {
174  std::cout << std::left << std::setw (10) << "extra:"
175  << "\"" << cmd.GetExtraNonOption (i) << "\""
176  << std::endl;
177  }
178 
179 
180  return 0;
181 }
cmd
Definition: second.py:35
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:877
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:213
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool SetCbArg(std::string val)
Function to illustrate command line arguments handled by a Callback function.
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:824