src/core/examples/command-line-example.cc
changeset 10865 23eb0f6c139a
parent 10861 40a485317b1d
child 10866 e4b913c6fe41
equal deleted inserted replaced
10864:d3887343ddb3 10865:23eb0f6c139a
    26 
    26 
    27 
    27 
    28 using namespace ns3;
    28 using namespace ns3;
    29 
    29 
    30 
    30 
    31 std::string g_cbArg="cbArg default";
    31 std::string g_cbArg = "cbArg default";
    32 
    32 
    33 bool SetCbArg (std::string val)
    33 bool SetCbArg (std::string val)
    34 {
    34 {
    35   g_cbArg = val;
    35   g_cbArg = val;
    36   return true;
    36   return true;
    41 {
    41 {
    42 
    42 
    43   int         intArg  = 1;
    43   int         intArg  = 1;
    44   bool        boolArg = false;
    44   bool        boolArg = false;
    45   std::string strArg  = "strArg default";
    45   std::string strArg  = "strArg default";
       
    46   // Attribute path
       
    47   const std::string attrClass = "ns3::RandomVariableStream";
       
    48   const std::string attrName  = "Antithetic";
       
    49   const std::string attrPath  = attrClass + "::" + attrName;
       
    50  
       
    51   // Cache the initial values.  Normally one wouldn't do this,
       
    52   // but we want to demonstrate that CommandLine has changed them.
       
    53   const int intDef = intArg;
       
    54   const bool boolDef = boolArg;
       
    55   const std::string strDef = strArg;
       
    56   const std::string cbDef  = g_cbArg;
       
    57   // Look up default value for attribute
       
    58   const TypeId tid = TypeId::LookupByName (attrClass);
       
    59   std::string attrDef;
       
    60   {
       
    61     struct TypeId::AttributeInformation info;
       
    62     tid.LookupAttributeByName (attrName, &info);
       
    63     attrDef = info.initialValue->SerializeToString (info.checker);
       
    64   }
       
    65   
    46   
    66   
    47   CommandLine cmd;
    67   CommandLine cmd;
    48   cmd.Usage ("CommandLine example program.\n"
    68   cmd.Usage ("CommandLine example program.\n"
    49              "\n"
    69              "\n"
    50              "This little program demonstrates how to use CommandLine.");
    70              "This little program demonstrates how to use CommandLine.");
    51   cmd.AddValue ("intArg",  "an int argument",       intArg);
    71   cmd.AddValue ("intArg",  "an int argument",       intArg);
    52   cmd.AddValue ("boolArg", "a bool argument",       boolArg);
    72   cmd.AddValue ("boolArg", "a bool argument",       boolArg);
    53   cmd.AddValue ("strArg",  "a string argument",     strArg);
    73   cmd.AddValue ("strArg",  "a string argument",     strArg);
    54   cmd.AddValue ("anti",    "ns3::RandomVariableStream::Antithetic");
    74   cmd.AddValue ("anti",    attrPath);
    55   cmd.AddValue ("cbArg",   "a string via callback", MakeCallback (SetCbArg));
    75   cmd.AddValue ("cbArg",   "a string via callback", MakeCallback (SetCbArg));
    56   cmd.Parse (argc, argv);
    76   cmd.Parse (argc, argv);
    57 
    77 
    58   std::cout << std::left
    78   // Show initial values:
    59             << std::setw (10) << "intArg:"         << intArg           << std::endl;
    79   std::cout << std::endl;
    60   std::cout << std::setw (10) << "boolArg:"
    80   std::cout << cmd.GetName () << ":" << std::endl;
    61             << std::boolalpha                      << boolArg
    81   std::cout << "Initial values:" << std::endl;
    62             << std::noboolalpha                                        << std::endl;
       
    63   
    82   
    64   std::cout << std::setw (10) << "strArg:" << "\"" << strArg  << "\""  << std::endl;
    83   std::cout << std::left << std::setw (10) << "intArg:"
    65   std::cout << std::setw (10) << "cbArg:"  << "\"" << g_cbArg << "\""  << std::endl;
    84             <<                    intDef
       
    85             << std::endl;
       
    86   std::cout << std::setw (10)              << "boolArg:"
       
    87             << std::boolalpha  << boolDef  << std::noboolalpha
       
    88             << std::endl;
       
    89   
       
    90   std::cout << std::setw (10)              << "strArg:"
       
    91             << "\""            << strDef   << "\""
       
    92             << std::endl;
       
    93 
       
    94   // Look up new default value for attribute
       
    95   {
       
    96     struct TypeId::AttributeInformation info;
       
    97     tid.LookupAttributeByName (attrName, &info);
       
    98   
       
    99     std::cout << std::setw (10)            << "anti:"
       
   100               << "\""
       
   101               << info.initialValue->SerializeToString (info.checker)
       
   102               << "\""
       
   103               << std::endl;
       
   104   }
       
   105   std::cout << std::setw (10)              << "cbArg:"
       
   106             << "\""            << cbDef    << "\""
       
   107             << std::endl;
       
   108   std::cout << std::endl;
       
   109 
       
   110 
       
   111   // Show final values
       
   112   std::cout << std::left << std::setw (10) << "intArg:"
       
   113             <<                    intArg
       
   114             << std::endl;
       
   115   std::cout << std::setw (10)              << "boolArg:"
       
   116             << std::boolalpha  << boolArg
       
   117             << std::noboolalpha
       
   118             << std::endl;
       
   119   
       
   120   std::cout << std::setw (10)              << "strArg:"
       
   121             << "\""            << strArg   << "\""
       
   122             << std::endl;
       
   123 
       
   124   // Look up new default value for attribute
       
   125   {
       
   126     struct TypeId::AttributeInformation info;
       
   127     tid.LookupAttributeByName (attrName, &info);
       
   128   
       
   129     std::cout << std::setw (10)            << "anti:"
       
   130               << "\""
       
   131               << info.initialValue->SerializeToString (info.checker)
       
   132               << "\""
       
   133               << std::endl;
       
   134   }
       
   135   std::cout << std::setw (10)              << "cbArg:"
       
   136             << "\""            << g_cbArg  << "\""
       
   137             << std::endl;
    66 
   138 
    67   return 0;
   139   return 0;
    68 }
   140 }