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 
71  // Cache the initial values. Normally one wouldn't do this,
72  // but we want to demonstrate that CommandLine has changed them.
73  const int intDef = intArg;
74  const bool boolDef = boolArg;
75  const std::string strDef = strArg;
76  const std::string cbDef = g_cbArg;
77  // Look up default value for attribute
78  const TypeId tid = TypeId::LookupByName (attrClass);
79  std::string attrDef;
80  {
81  struct TypeId::AttributeInformation info;
82  tid.LookupAttributeByName (attrName, &info);
83  attrDef = info.originalInitialValue->SerializeToString (info.checker);
84  }
85 
86 
88  cmd.Usage ("CommandLine example program.\n"
89  "\n"
90  "This little program demonstrates how to use CommandLine.");
91  cmd.AddValue ("intArg", "an int argument", intArg);
92  cmd.AddValue ("boolArg", "a bool argument", boolArg);
93  cmd.AddValue ("strArg", "a string argument", strArg);
94  cmd.AddValue ("anti", attrPath);
95  cmd.AddValue ("cbArg", "a string via callback", MakeCallback (SetCbArg));
96  cmd.Parse (argc, argv);
97 
98  // Show initial values:
99  std::cout << std::endl;
100  std::cout << cmd.GetName () << ":" << std::endl;
101  std::cout << "Initial values:" << std::endl;
102 
103  std::cout << std::left << std::setw (10) << "intArg:"
104  << intDef
105  << std::endl;
106  std::cout << std::setw (10) << "boolArg:"
107  << std::boolalpha << boolDef << std::noboolalpha
108  << std::endl;
109 
110  std::cout << std::setw (10) << "strArg:"
111  << "\"" << strDef << "\""
112  << std::endl;
113  std::cout << std::setw (10) << "anti:"
114  << "\"" << attrDef << "\""
115  << std::endl;
116  std::cout << std::setw (10) << "cbArg:"
117  << "\"" << cbDef << "\""
118  << std::endl;
119  std::cout << std::endl;
120 
121 
122  // Show final values
123  std::cout << "Final values:" << std::endl;
124  std::cout << std::left << std::setw (10) << "intArg:"
125  << intArg
126  << std::endl;
127  std::cout << std::setw (10) << "boolArg:"
128  << std::boolalpha << boolArg
129  << std::noboolalpha
130  << std::endl;
131 
132  std::cout << std::setw (10) << "strArg:"
133  << "\"" << strArg << "\""
134  << std::endl;
135 
136  // Look up new default value for attribute
137  {
138  struct TypeId::AttributeInformation info;
139  tid.LookupAttributeByName (attrName, &info);
140 
141  std::cout << std::setw (10) << "anti:"
142  << "\""
143  << info.initialValue->SerializeToString (info.checker)
144  << "\""
145  << std::endl;
146  }
147  std::cout << std::setw (10) << "cbArg:"
148  << "\"" << g_cbArg << "\""
149  << std::endl;
150 
151  return 0;
152 }
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:876
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:498
std::string g_cbArg
Global variable to illustrate command line arguments handled by a Callback function.
void Parse(int argc, char *argv[])
Parse the program arguments.
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:823