美文网首页NS3(Network Simulator)
NS3 waf运行命令以及命令行解析的使用说明

NS3 waf运行命令以及命令行解析的使用说明

作者: shawn168 | 来源:发表于2017-06-25 20:25 被阅读1163次

    在ubuntu环境下,打开终端,输入:

    $ waf --help
    waf [command] [options]
    

    即会出现大量的关于waf的命令行命令以及参数信息。

    命令行模式下运行程序,可设置参数。
    参数设置有两行类型参数。一种是程序中的参数(程序参数),另一种是普通参数

    例如:

    ./waf --run="wifi-simple-learn-tcptest --help"
    
    ns3.26-wifi-simple-learn-tcptest-debug [Program Arguments] [General Arguments]
    

    程序参数

    这种参数通过在代码中设置的,通过CommandLine类完成解析,并设置参数的值。

    Program Arguments:
        --phyMode:            Wifi Phy mode [OfdmRate6Mbps]
        --delta:              time offset (microseconds) for interfering signal [0]
        --PpacketSize:        size of application packet sent [1000]
        --IpacketSize:        size of interfering packet sent [1000]
        --verbose:            turn on all WifiNetDevice log components [false]
        --packetEnablePrint:  turn on print packet info [false]
    
    

    以上参数是在ns3.26-wifi-simple-learn-tcptest-debug代码文件中设置的,以上输出信息,
    包含三部分:命令行参数名,参数说明,以及[]中括号中给出的参数默认值。

    用法:

    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --packetEnablePrint=true"
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --packetEnablePrint=false"(这样的设置,等于默认值)
    

    通用参数

    这种参数用法:

    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --PrintHelp"
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --PrintGroups"
    
    //以wifi示例
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --PrintGroup=wifi"
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --PrintTypeIds"
    
    //与PrintHelp相同
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --help"
    
    //以ns3::AdhocWifiMac示例
    ./waf --run "examples/wireless/wifi-simple-learn-tcptest --PrintAttributes=ns3::AdhocWifiMac"
    

    通用参数有:

        --PrintGlobals:              Print the list of globals.     打印输出可以设置的全局变量
        --PrintGroups:               Print the list of groups.      打印输出当前程序依赖的模块。
                                                                    这些模块是在脚本配置文件中对当前程序的配置模块。
        --PrintGroup=[group]:        Print all TypeIds of group.    打印输出指定模块的全部TypeId。
        --PrintTypeIds:              Print all TypeIds.             打印所有TypeId
        --PrintAttributes=[typeid]:  Print all attributes of typeid.打印指定TypeId的属性
        --PrintHelp:                 Print this help message.       打印帮助信息
    
    

    用法步骤

    第一步:
    在代码中使用Command类,定义程序参数,并解析。

    CommandLine cmd;
      cmd.Usage ("CommandLine example program.\n"
                 "\n"
                 "This little program demonstrates how to use CommandLine.");
      cmd.AddValue ("intArg",  "an int argument",       intArg);
      cmd.AddValue ("boolArg", "a bool argument",       boolArg);
      cmd.AddValue ("strArg",  "a string argument",     strArg);
      cmd.AddValue ("anti",    attrPath);
      cmd.AddValue ("cbArg",   "a string via callback", MakeCallback (SetCbArg));
      cmd.Parse (argc, argv);
    

    Usage 函数,输出参数信息。

    AddValue函数,有三个参数:
    第一个参数:定义在命令行使用的字符串
    第二个参数:对变量意义说明
    第三个参数:将命令行设置的值,设置到对应的变量。

    Parse 函数完成解析命令行参数。

    第二步:

    在命令行使用AddValue函数的第一个参数字符串,设置变量对应的值。
    例如:

    ./waf --run="command-line-example --intArg=2 --boolArg --strArg=Hello --cbArg=World --help"
    

    说明

    void
    CommandLine::AddValue (const std::string &name,
                           const std::string &help,
                           Callback<bool, std::string> callback)
    
    
    void
    CommandLine::AddValue (const std::string &name,
                           const std::string &attributePath)
    
    void 
    CommandLine::AddValue (const std::string &name,
                           const std::string &help,
                           T &value)
    

    CommandLine类有这三个AddValue 的函数可以添加,对参数值进行设定。

    第一个函数:对指定的参数设置回调函数。这里可以参看NS3 Callback简单说明

    第二个函数:对指定的值参数设置属性。这里面的属性参数一般都是NS3本身的属性参数。

    第三个函数:对指定的参数设置值。最简单的一种参数值设置。

    示例代码:

      const std::string attrClass = "ns3::RandomVariableStream";
      const std::string attrName  = "Antithetic";
      const std::string attrPath  = attrClass + "::" + attrName;
     
      // Cache the initial values.  Normally one wouldn't do this,
      // but we want to demonstrate that CommandLine has changed them.
      const int intDef = intArg;
      const bool boolDef = boolArg;
      const std::string strDef = strArg;
      const std::string cbDef  = g_cbArg;
      // Look up default value for attribute
      const TypeId tid = TypeId::LookupByName (attrClass);
      std::string attrDef;
      {
        struct TypeId::AttributeInformation info;
        tid.LookupAttributeByName (attrName, &info);
        attrDef = info.originalInitialValue->SerializeToString (info.checker);
      }
      
      
      CommandLine cmd;
      cmd.Usage ("CommandLine example program.\n"
                 "\n"
                 "This little program demonstrates how to use CommandLine.");
      cmd.AddValue ("intArg",  "an int argument",       intArg);
      cmd.AddValue ("boolArg", "a bool argument",       boolArg);
    
      //对参数值设定
      cmd.AddValue ("strArg",  "a string argument",     strArg);
    
      //对路径属性进行设置
      cmd.AddValue ("anti",    attrPath);
    
      //对参数设置回调函数
      cmd.AddValue ("cbArg",   "a string via callback", MakeCallback (SetCbArg));
    

    实例:

    例如代码:(代码位置:/core/examples/command-line-example.cc)

    #include <iostream>
    #include <iomanip>
    #include <string>
    
    #include "ns3/core-module.h"
    
    using namespace ns3;
    
    std::string g_cbArg = "cbArg default";
    
    bool SetCbArg (std::string val)
    {
      g_cbArg = val;
      return true;
    }
    
    int main (int argc, char *argv[])
    {
      int         intArg  = 1;
      bool        boolArg = false;
      std::string strArg  = "strArg default";
      // Attribute path
      const std::string attrClass = "ns3::RandomVariableStream";
      const std::string attrName  = "Antithetic";
      const std::string attrPath  = attrClass + "::" + attrName;
     
      const int intDef = intArg;
      const bool boolDef = boolArg;
      const std::string strDef = strArg;
      const std::string cbDef  = g_cbArg;
      // Look up default value for attribute
      const TypeId tid = TypeId::LookupByName (attrClass);
      std::string attrDef;
      {
        struct TypeId::AttributeInformation info;
        tid.LookupAttributeByName (attrName, &info);
        attrDef = info.originalInitialValue->SerializeToString (info.checker);
      }
      
      CommandLine cmd;
      cmd.Usage ("CommandLine example program.\n"
                 "\n"
                 "This little program demonstrates how to use CommandLine.");
      cmd.AddValue ("intArg",  "an int argument",       intArg);
      cmd.AddValue ("boolArg", "a bool argument",       boolArg);
      cmd.AddValue ("strArg",  "a string argument",     strArg);
      cmd.AddValue ("anti",    attrPath);
      cmd.AddValue ("cbArg",   "a string via callback", MakeCallback (SetCbArg));
      cmd.Parse (argc, argv);
    
      // 输出初始值:
      std::cout << std::endl;
      std::cout << cmd.GetName () << ":" << std::endl;
      std::cout << "Initial values:" << std::endl;
      
      std::cout << std::left << std::setw (10) << "intArg:"
                <<                    intDef
                << std::endl;
      std::cout << std::setw (10)              << "boolArg:"
                << std::boolalpha  << boolDef  << std::noboolalpha
                << std::endl;
      
      std::cout << std::setw (10)              << "strArg:"
                << "\""            << strDef   << "\""
                << std::endl;
      std::cout << std::setw (10)              << "anti:"
                << "\""            << attrDef  << "\""
                << std::endl;
      std::cout << std::setw (10)              << "cbArg:"
                << "\""            << cbDef    << "\""
                << std::endl;
      std::cout << std::endl;
    
      // 输出最终值:
      std::cout << "Final values:" << std::endl;
      std::cout << std::left << std::setw (10) << "intArg:"
                <<                    intArg
                << std::endl;
      std::cout << std::setw (10)              << "boolArg:"
                << std::boolalpha  << boolArg
                << std::noboolalpha
                << std::endl;
      
      std::cout << std::setw (10)              << "strArg:"
                << "\""            << strArg   << "\""
                << std::endl;
    
      // Look up new default value for attribute
      {
        struct TypeId::AttributeInformation info;
        tid.LookupAttributeByName (attrName, &info);
      
        std::cout << std::setw (10)            << "anti:"
                  << "\""
                  << info.initialValue->SerializeToString (info.checker)
                  << "\""
                  << std::endl;
      }
      std::cout << std::setw (10)              << "cbArg:"
                << "\""            << g_cbArg  << "\""
                << std::endl;
      return 0;
    }
    

    命令行运行代码:

    ./waf --run="command-line-example"
    
    ./waf --run="command-line-example --intArg=2 --boolArg --strArg=Hello --cbArg=World"
    
    ./waf --run="command-line-example --help"
    
    

    第一行命令,不加参数运行代码

    第二行命令,加程序参数运行代码

    第三行命令,加通用参数运行代码

    相关文章

      网友评论

        本文标题:NS3 waf运行命令以及命令行解析的使用说明

        本文链接:https://www.haomeiwen.com/subject/xydgnttx.html