首页 > 其他分享 >OFtutorial02_commandLineArgumentsAndOptions

OFtutorial02_commandLineArgumentsAndOptions

时间:2024-08-09 17:09:54浏览次数:4  
标签:选项 someScalar commandLineArgumentsAndOptions args argument someWord OFtutorial02

OFtutorial2.C

argList类

如图

包含很多函数,常用的addNote(输出字符串), noParallel(去掉基类中的并行选项), addBoolOption, addOption(增加选项)

源码

#include "fvCFD.H"
#argc即argument count的缩写,保存程序运行时传递给主函数的参数个数;argv即argument vector的缩写,保存程序运行时传递给主函数的具体参数的字符型指针,每个指针都指向一个具体的参数。
int main(int argc, char *argv[])
{
    // ===
    // Define the help message for this application
    argList::addNote
    (
        "Demonstrates how to handle command line options.\n"
        "\n"
        "Input arguments:\n"
        "----------------\n"
        "  someWord - does stuff\n"
        "  someScalar - does more things\n"
    );

    // prepare argument list
    argList::noParallel();#去掉并行选项
    argList::validArgs.append("someWord");#增加someWord选项
    argList::validArgs.append("someScalar");#增加someScalar选项

    // prepare options
    argList::addOption // string variable#增加输出字典路径选项
    (
        "dict",#定义选项
        "word",#定义选项后面需要跟的字符串,这里表示字典名称
        "Path to an additional dictionary (not really used now)"给定命令说明
    );

    argList::addBoolOption // on/off depending on whether option is given or not#从此处可见选项后的字符不必需,但说明必需
    (
        "someSwitch",
        "Switches from A to B"
    );

    argList::addOption // integer variable
    (
        "someInt",
        "label",
        "Optional integer"
    );

    // ===
    // create argument list
    // This is normally defined inside setRootCase.H
    // #include "setRootCase.H"
    Foam::argList args(argc, argv);
    if (!args.checkRootCase())
    {
        Foam::FatalError.exit();
    }

    // ===
    // read arguments
    const word someWord = args[1];
    // NOTE: the built-in method for converting strings to other data types#在命令行输入的是字符串,但是这个someScalar是数值,不能直接=args[2],需要将字符串转换为其他数据类型(如scalar)的内置方法
    const scalar someScalar = args.argRead<scalar>(2);

    Info << "Got argument word " << someWord << " and scalar " << someScalar << endl;

    // ===
    // read options
    // default path to some dictionary
    fileName dictPath("./system/defaultDict");#定义读取信息的文件地址

    // conditional execution based on an option being passed
    if (args.optionFound("dict"))
    {
        args.optionReadIfPresent("dict", dictPath);
        Info << "Got an override flag for dictionary path" << endl;
    }
    Info << "Would read dict from " << dictPath << endl;

    // switch option
    const bool someConstBool = args.optionFound("someSwitch");
    Info << "Boolean switch set to " << someConstBool << endl;

    // numeric value option - same as string variables really
    label someInt(0);
    args.optionReadIfPresent("someInt", someInt);
    Info << "Integer option value " << someInt << endl;
    
    Info<< "End\n" << endl;

    return 0;
}

Allwmake、Allwclean、Make

这三个文件(目录)与此前章节差别不大,故此处不再赘述

testcase

该目录与前章区别也不大,system与constant中的内容未读取
值得注意的是

Allrun

ofTutorial2 someWordIPassed 5. -someSwitch -someInt 2 -dict path_to_nowhere_in_particular

其给定了求解器所需的初始数据

小结

该章我们学习了如何给求解器增加“help”选项

标签:选项,someScalar,commandLineArgumentsAndOptions,args,argument,someWord,OFtutorial02
From: https://www.cnblogs.com/ouqiyo/p/18351055

相关文章