A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
command-line-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 Lawrence Livermore National Laboratory
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Peter D. Barnes, Jr. <pdbarnes@llnl.gov>
7 */
8
9#include "ns3/core-module.h"
10
11#include <iomanip>
12#include <iostream>
13#include <string>
14
15/**
16 * \file
17 * \ingroup core-examples
18 * \ingroup commandline
19 * Example program illustrating use of ns3::CommandLine.
20 */
21
22using namespace ns3;
23
24namespace
25{
26
27/**
28 * Global variable to illustrate command line arguments handled by a
29 * Callback function.
30 */
31std::string g_cbArg = "cbArg default";
32
33/**
34 * Function to illustrate command line arguments handled by a
35 * Callback function.
36 *
37 * \param [in] val New value for \pname{g_cbArg}.
38 * \returns \c true.
39 */
40bool
41SetCbArg(const std::string& val)
42{
43 g_cbArg = val;
44 return true;
45}
46
47} // unnamed namespace
48
49/**
50 * Print a row containing the name, the default
51 * and the final values of an argument.
52 *
53 * \param [in] label The argument label.
54 * \param [in] defaultValue The default value of the argument.
55 * \param [in] finalValue The final value of the argument.
56 *
57 */
58#define DefaultFinal(label, defaultValue, finalValue) \
59 std::left << std::setw(20) << label + std::string(":") << std::setw(20) << defaultValue \
60 << finalValue << "\n"
61
62int
63main(int argc, char* argv[])
64{
65 // Plain old data options
66 int intArg = 1;
67 bool boolArg = false;
68 std::string strArg = "strArg default";
69
70 // Attribute path option
71 const std::string attrClass = "ns3::RandomVariableStream";
72 const std::string attrName = "Antithetic";
73 const std::string attrPath = attrClass + "::" + attrName;
74
75 // char* buffer option
76 constexpr int CHARBUF_SIZE = 10;
77 char charbuf[CHARBUF_SIZE] = "charstar";
78
79 // Non-option arguments
80 int nonOpt1 = 1;
81 int nonOpt2 = 1;
82
83 // Cache the initial values. Normally one wouldn't do this,
84 // but we want to demonstrate that CommandLine has changed them.
85 const int intDef = intArg;
86 const bool boolDef = boolArg;
87 const std::string strDef = strArg;
88 const std::string cbDef = g_cbArg;
89 // Look up default value for attribute
90 const TypeId tid = TypeId::LookupByName(attrClass);
91 std::string attrDef;
92 {
94 tid.LookupAttributeByName(attrName, &info);
95 attrDef = info.originalInitialValue->SerializeToString(info.checker);
96 }
97 const std::string charbufDef{charbuf};
98 const int nonOpt1Def = nonOpt1;
99 const int nonOpt2Def = nonOpt2;
100
101 CommandLine cmd(__FILE__);
102 cmd.Usage("CommandLine example program.\n"
103 "\n"
104 "This little program demonstrates how to use CommandLine.");
105 cmd.AddValue("intArg", "an int argument", intArg);
106 cmd.AddValue("boolArg", "a bool argument", boolArg);
107 cmd.AddValue("strArg", "a string argument", strArg);
108 cmd.AddValue("anti", attrPath);
109 cmd.AddValue("cbArg", "a string via callback", MakeCallback(SetCbArg));
110 cmd.AddValue("charbuf", "a char* buffer", charbuf, CHARBUF_SIZE);
111 cmd.AddNonOption("nonOpt1", "first non-option", nonOpt1);
112 cmd.AddNonOption("nonOpt2", "second non-option", nonOpt2);
113 cmd.Parse(argc, argv);
114
115 // Show what happened
116 std::cout << std::endl;
117 std::cout << cmd.GetName() << ":" << std::endl;
118
119 // Print the source version used to build this example
120 std::cout << "Program Version: ";
121 cmd.PrintVersion(std::cout);
122 std::cout << std::endl;
123
124 std::cout << "Argument Initial Value Final Value\n"
125 << std::left << std::boolalpha;
126
127 std::cout << DefaultFinal("intArg", intDef, intArg) //
128 << DefaultFinal("boolArg",
129 (boolDef ? "true" : "false"),
130 (boolArg ? "true" : "false")) //
131 << DefaultFinal("strArg", "\"" + strDef + "\"", "\"" + strArg + "\"");
132
133 // Look up new default value for attribute
134 std::string antiArg;
135 {
137 tid.LookupAttributeByName(attrName, &info);
138 antiArg = info.initialValue->SerializeToString(info.checker);
139 }
140
141 std::cout << DefaultFinal("anti", "\"" + attrDef + "\"", "\"" + antiArg + "\"")
142 << DefaultFinal("cbArg", cbDef, g_cbArg)
143 << DefaultFinal("charbuf",
144 "\"" + charbufDef + "\"",
145 "\"" + std::string(charbuf) + "\"")
146 << DefaultFinal("nonOpt1", nonOpt1Def, nonOpt1)
147 << DefaultFinal("nonOpt2", nonOpt2Def, nonOpt2) << std::endl;
148
149 std::cout << std::setw(40)
150 << "Number of extra non-option arguments:" << cmd.GetNExtraNonOptions() << std::endl;
151
152 for (std::size_t i = 0; i < cmd.GetNExtraNonOptions(); ++i)
153 {
154 std::cout << DefaultFinal("extra non-option " + std::to_string(i),
155 "",
156 "\"" + cmd.GetExtraNonOption(i) + "\"");
157 }
158 std::cout << std::endl;
159
160#undef DefaultFinal
161
162 return 0;
163}
Parse command-line arguments.
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
bool LookupAttributeByName(std::string name, AttributeInformation *info, bool permissive=false) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition type-id.cc:968
#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.
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:684
Attribute implementation.
Definition type-id.h:70
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition type-id.h:78
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:84
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition type-id.h:80