c++ - 创建我的 .exe 的批处理版本,它采用命令行参数(BCB 4.0 中的 ParamCount()、ParamStr() 和 FindCmdLineSwitch)

标签 c++ c++builder borland-c++ c++builder-6

对于我在 Borland C++ Builder 4.0 中创建的程序,我希望能够创建一个可以设置批处理脚本的版本。批处理 srcipt 将调用我的 .exe(不启动主窗体窗口),这将导致使用批处理文件中指定的输入来执行我的程序的主要过程。生成输出后,程序将关闭。

批处理脚本的前三个参数将指定三个主要输入文件(否则用按钮加载的文件)的位置,设置一个开关来定义是对单个案例还是对多个案例进行插值(类似于 -m 或 -s)。如果前者为真,程序将读取第四种输入文件的位置。在后者的情况下,它将读取另一个 csv,它给出了第四种输入类型的多个输入文件的位置。批处理文件还将定义输出位置和输出文件名。

根据我目前在此处和不同论坛上阅读的内容,我认为实现此目的的最简单方法是使用 ParamCount() 和 ParamStr() 以及 FindCmdLineSwitch。仍然有点模糊我到底打算如何使用这些(我为我的无知道歉,但这不仅是我的第一个 BCB 项目,也是我用 C++ 编码和创建 Windows GUI 的第一次真实体验)...据我了解,我可以以与此处描述的方式类似的方式使用这些 http://docwiki.embarcadero.com/CodeExamples/Seattle/en/ParamCount_(C%2B%2B) .

有几件事我不知道:

  1. 我应该在程序的什么地方放置 ParamCount() 和 ParamStr() 部分来检查我是否从命令行/使用批处理文件启动了 .exe?这里建议将它放在主 .cpp 文件中,即初始化表单的文件 http://www.borlandtalk.com/running-command-line-parameters-vt17942.html .我正在考虑这样做并从我的主要 UnitSomething.cpp 中调用正确的函数。或者我应该将这些功能放在其他地方吗?
  2. 批处理文件会是什么样子?里面的参数是怎么分开的?只是空格?比如说,在我将新部分包含在我的脚本中之后,我可以创建一个看起来像这样的批处理脚本吗?

    "C:/Program Files/myprogram/myprogram.exe""第一个输入的位置""第二个输入的位置"-m 等

    (我以前在批处理模式下使用过 Ansys CFX,这是一个 CFD 工具,例如它有开关来定义哪个文件是定义文件 [-def] 和初始化文件 [-ini])。

  3. 与上述相关,开关是如何出现的?什么时候应该使用它们?例如,当我想为第一个输入定义一个位置时,它前面是否应该有一个开关,比如-inp1?我在这里看到一个例子 Selection of Forms just after program execution但我不确定这与简单的 ParamStr 有何不同?更具体地说,如何以及何时使用 FindCmdLineSwitch?

  4. 最后,使用以上三个函数中的任何一个,我是否必须更改 WINAPI WinMain() 调用参数中的任何内容?

谢谢。

最佳答案

Where in my program should I place the ParamCount() and ParamStr() parts which check whether I've launched the .exe from the command line/with a batch file?

在项目的 WinMain() 函数中,就像我在评论中告诉你的那样。例如:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    if (ParamCount() >= 5)
    {
        String InputFile1 = ParamStr(1);
        String InputFile2 = ParamStr(2);
        String InputFile3 = ParamStr(3);
        String Interpolation = ParamStr(4);

        int index = 5;
        if (Interpolation == "-s")
        {
            String InputFile4 = ParamStr(index++);
            // load file as needed...
        }
        else
        {
            // load CSV as needed...
        }

        String OutputFile = ParamStr(index);

        // process files as needed...
    }
    else
    {
        // normal GUI code here...
    }

    return 0;
}

it is suggested to place it in the main .cpp file, the one which initializes the forms

是的。

How would the batch file look like?

例如:

myprogram.exe "inputfile1" "inputfile2" "inputfile3" -m "outputfile"

myprogram.exe "inputfile1" "inputfile2" "inputfile3" -s "inputfile4" "outputfile"

How are the parameters separated in it? Just by spaces?

是的。其中有空格的参数需要用引号引起来。

Say, after I've included the new part in my script, can I create a batch script which looks something like this?

是的。

Related to the aboove, how do switches come into the picture? When should they be used? For example, when I want to define a location for the first input, should there be a switch before it, something like -inp1?

你可以这样做,但在这种情况下没有必要,因为参数有固定的位置。如果你想命名你的开关,你可以做更多类似这样的事情:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    int count = ParamCount();
    if (count > 0)
    {
        String InputFile1;
        String InputFile2;
        String InputFile3;
        String InputFile4;
        String Interpolation;
        String OutputFile;

        int idx = 1;
        while (idx <= count)
        {
            String param = ParamStr(idx++);

            if (param == "-inp1")
                InputFile1 = ParamStr(idx++);

            else if (param == "-inp2")
                InputFile2 = ParamStr(idx++);

            else if (param == "-inp3")
                InputFile3 = ParamStr(idx++);

            else if (param == "-s")
            {
                Interpolation = param;
                InputFile4 = ParamStr(idx++);
            }

            else if (param == "-m")
                Interpolation = param;

            else if (param == "-out")
                OutputFile = ParamStr(idx++);
        }

        // process files as needed...
    }
    else
    {
        // normal GUI code here...
    }

    return 0;
}

I see an example here "Selection of Forms just after program execution" but I'm not sure how does this differ from a simple ParamStr? More specifically, how and when do I use FindCmdLineSwitch?

FindCmdLineSwitch() 在整个命令行中搜索指定的开关。开关本身可以存在于命令行中的任何位置。当您不知道或不关心参数的顺序时,这很好,您只想知道开关是否存在。 FindCmdLineSwitch() 在开关的位置很重要或其他参数与开关相关联时没有用,因为 FindCmdLineSwitch() 不会告诉您它所在的位置找到了开关。

Finally, using any of the above three functions, do I have to change anything in the WINAPI WinMain() call parameters?

没有。但是,ParamStr() 确实存在一些已知的解析问题(参见 QC #3946),并且 FindCmdLineSwitch() 仅限于两种格式(“/switch”和“- switch”,它无法处理诸如“/switch:value”、“switch=value”等)。在这些情况下,如果您发现需要手动解析命令行,您可以使用 WinMain()lpCmdLine 参数(尽管 GetCommandLine() 通常是更好的选择)。

关于c++ - 创建我的 .exe 的批处理版本,它采用命令行参数(BCB 4.0 中的 ParamCount()、ParamStr() 和 FindCmdLineSwitch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974727/

相关文章:

c++ - 自定义 std::allocator 用于替换了 operator new 的类

c++ - C/C++中随机数生成器的实现

c++ - 为什么我在打印这个结构指针时出错?

c++builder - c++ Builder xe5 检测到错误(LME288)

c++ - 使用 TColorListBox 更改矩形的颜色

C++ Visual Studio "Disgusting Macro Hack"编译问题

c++ - 为什么我不能用 reset() 删除 unique_ptr<char[]>?

c++ - Borland C++ Builder 中的 scanf

c++ - 在 Borland C++ 编译器 5.5 中询问文件 .obj 和 .tds

c++ - 用VS编译器在Qt中编译dll以便在Borland中使用