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 
92  CommandLine cmd (__FILE__);
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", "second 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 
109  //Print the source version used to build this example
110  std::cout << "Program Version: ";
111  cmd.PrintVersion (std::cout);
112  std::cout << std::endl;
113 
114  std::cout << "Initial values:" << std::endl;
115 
116  std::cout << std::left << std::setw (10) << "intArg:"
117  << intDef
118  << std::endl;
119  std::cout << std::setw (10) << "boolArg:"
120  << std::boolalpha << boolDef << std::noboolalpha
121  << std::endl;
122 
123  std::cout << std::setw (10) << "strArg:"
124  << "\"" << strDef << "\""
125  << std::endl;
126  std::cout << std::setw (10) << "anti:"
127  << "\"" << attrDef << "\""
128  << std::endl;
129  std::cout << std::setw (10) << "cbArg:"
130  << "\"" << cbDef << "\""
131  << std::endl;
132  std::cout << std::left << std::setw (10) << "nonOpt1:"
133  << nonOpt1Def
134  << std::endl;
135  std::cout << std::left << std::setw (10) << "nonOpt2:"
136  << nonOpt2Def
137  << std::endl;
138  std::cout << std::endl;
139 
140 
141  // Show final values
142  std::cout << "Final values:" << std::endl;
143  std::cout << std::left << std::setw (10) << "intArg:"
144  << intArg
145  << std::endl;
146  std::cout << std::setw (10) << "boolArg:"
147  << std::boolalpha << boolArg << std::noboolalpha
148  << std::endl;
149 
150  std::cout << std::setw (10) << "strArg:"
151  << "\"" << strArg << "\""
152  << std::endl;
153 
154  // Look up new default value for attribute
155  {
156  struct TypeId::AttributeInformation info;
157  tid.LookupAttributeByName (attrName, &info);
158 
159  std::cout << std::setw (10) << "anti:"
160  << "\""
161  << info.initialValue->SerializeToString (info.checker)
162  << "\""
163  << std::endl;
164  }
165  std::cout << std::setw (10) << "cbArg:"
166  << "\"" << g_cbArg << "\""
167  << std::endl;
168  std::cout << std::left << std::setw (10) << "nonOpt1:"
169  << nonOpt1
170  << std::endl;
171  std::cout << std::left << std::setw (10) << "nonOpt2:"
172  << nonOpt2
173  << std::endl;
174  std::cout << std::left << "Number of extra non-option arguments:"
175  << cmd.GetNExtraNonOptions ()
176  << std::endl;
177 
178  for (std::size_t i = 0; i < cmd.GetNExtraNonOptions (); ++i)
179  {
180  std::cout << std::left << std::setw (10) << "extra:"
181  << "\"" << cmd.GetExtraNonOption (i) << "\""
182  << std::endl;
183  }
184 
185 
186  return 0;
187 }
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:883
Attribute implementation.
Definition: type-id.h:77
Parse command-line arguments.
Definition: command-line.h:227
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
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830