A Discrete-Event Network Simulator
API
command-line-example.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Lawrence Livermore National Laboratory
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
18  */
19 
20 #include "ns3/core-module.h"
21 
22 #include <iomanip>
23 #include <iostream>
24 #include <string>
25 
33 using namespace ns3;
34 
35 namespace
36 {
37 
42 std::string g_cbArg = "cbArg default";
43 
51 bool
52 SetCbArg(const std::string& val)
53 {
54  g_cbArg = val;
55  return true;
56 }
57 
58 } // unnamed namespace
59 
69 #define DefaultFinal(label, defaultValue, finalValue) \
70  std::left << std::setw(20) << label + std::string(":") << std::setw(20) << defaultValue \
71  << finalValue << "\n"
72 
73 int
74 main(int argc, char* argv[])
75 {
76  // Plain old data options
77  int intArg = 1;
78  bool boolArg = false;
79  std::string strArg = "strArg default";
80 
81  // Attribute path option
82  const std::string attrClass = "ns3::RandomVariableStream";
83  const std::string attrName = "Antithetic";
84  const std::string attrPath = attrClass + "::" + attrName;
85 
86  // char* buffer option
87  constexpr int CHARBUF_SIZE = 10;
88  char charbuf[CHARBUF_SIZE] = "charstar";
89 
90  // Non-option arguments
91  int nonOpt1 = 1;
92  int nonOpt2 = 1;
93 
94  // Cache the initial values. Normally one wouldn't do this,
95  // but we want to demonstrate that CommandLine has changed them.
96  const int intDef = intArg;
97  const bool boolDef = boolArg;
98  const std::string strDef = strArg;
99  const std::string cbDef = g_cbArg;
100  // Look up default value for attribute
101  const TypeId tid = TypeId::LookupByName(attrClass);
102  std::string attrDef;
103  {
105  tid.LookupAttributeByName(attrName, &info);
106  attrDef = info.originalInitialValue->SerializeToString(info.checker);
107  }
108  const std::string charbufDef{charbuf};
109  const int nonOpt1Def = nonOpt1;
110  const int nonOpt2Def = nonOpt2;
111 
112  CommandLine cmd(__FILE__);
113  cmd.Usage("CommandLine example program.\n"
114  "\n"
115  "This little program demonstrates how to use CommandLine.");
116  cmd.AddValue("intArg", "an int argument", intArg);
117  cmd.AddValue("boolArg", "a bool argument", boolArg);
118  cmd.AddValue("strArg", "a string argument", strArg);
119  cmd.AddValue("anti", attrPath);
120  cmd.AddValue("cbArg", "a string via callback", MakeCallback(SetCbArg));
121  cmd.AddValue("charbuf", "a char* buffer", charbuf, CHARBUF_SIZE);
122  cmd.AddNonOption("nonOpt1", "first non-option", nonOpt1);
123  cmd.AddNonOption("nonOpt2", "second non-option", nonOpt2);
124  cmd.Parse(argc, argv);
125 
126  // Show what happened
127  std::cout << std::endl;
128  std::cout << cmd.GetName() << ":" << std::endl;
129 
130  // Print the source version used to build this example
131  std::cout << "Program Version: ";
132  cmd.PrintVersion(std::cout);
133  std::cout << std::endl;
134 
135  std::cout << "Argument Initial Value Final Value\n"
136  << std::left << std::boolalpha;
137 
138  std::cout << DefaultFinal("intArg", intDef, intArg) //
139  << DefaultFinal("boolArg",
140  (boolDef ? "true" : "false"),
141  (boolArg ? "true" : "false")) //
142  << DefaultFinal("strArg", "\"" + strDef + "\"", "\"" + strArg + "\"");
143 
144  // Look up new default value for attribute
145  std::string antiArg;
146  {
148  tid.LookupAttributeByName(attrName, &info);
149  antiArg = info.initialValue->SerializeToString(info.checker);
150  }
151 
152  std::cout << DefaultFinal("anti", "\"" + attrDef + "\"", "\"" + antiArg + "\"")
153  << DefaultFinal("cbArg", cbDef, g_cbArg)
154  << DefaultFinal("charbuf",
155  "\"" + charbufDef + "\"",
156  "\"" + std::string(charbuf) + "\"")
157  << DefaultFinal("nonOpt1", nonOpt1Def, nonOpt1)
158  << DefaultFinal("nonOpt2", nonOpt2Def, nonOpt2) << std::endl;
159 
160  std::cout << std::setw(40)
161  << "Number of extra non-option arguments:" << cmd.GetNExtraNonOptions() << std::endl;
162 
163  for (std::size_t i = 0; i < cmd.GetNExtraNonOptions(); ++i)
164  {
165  std::cout << DefaultFinal("extra non-option " + std::to_string(i),
166  "",
167  "\"" + cmd.GetExtraNonOption(i) + "\"");
168  }
169  std::cout << std::endl;
170 
171 #undef DefaultFinal
172 
173  return 0;
174 }
Parse command-line arguments.
Definition: command-line.h:232
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:893
#define DefaultFinal(label, defaultValue, finalValue)
Print a row containing the name, the default and the final values of an argument.
std::string g_cbArg
Global variable to illustrate command line arguments handled by a Callback function.
bool SetCbArg(const std::string &val)
Function to illustrate command line arguments handled by a Callback function.
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.h:25255
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
Attribute implementation.
Definition: type-id.h:81
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:89
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:91