[英]Specify global options for a Perforce command run using p4api
I am using the Perforce, aka Helix Core, C++ API to programmatically run a Perforce command.我正在使用 Perforce,又名 Helix Core,C++ API 以编程方式运行 Perforce 命令。 How do I specify a global option for the command?
如何为命令指定全局选项?
For example, I want to programmatically run the clients
command with several global options.例如,我想以编程方式运行带有多个全局选项的
clients
命令。 If run from a command shell, it would look like the following.如果从命令 shell 运行,它将如下所示。
p4 -z tag -F %client% clients -u mikef
The global options I want, -z
and -F
, are not ones that you can specify via environment variables, as far as I know.据我所知,我想要的全局选项
-z
和-F
不是您可以通过环境变量指定的选项。 But even if you could, I cannot rely on the user to set them.但即使你可以,我也不能依赖用户来设置它们。
On a lark, I added the global options to the argument array provided to the ClientApi
object.在云雀中,我将全局选项添加到提供给
ClientApi
object 的参数数组中。 For example:例如:
#include <p4/clientapi.h>
#include "CustomClientUser.h" // A class I derived from ClientUser
// Connect to server.
StrBuf msg;
Error e;
ClientApi client;
client.SetProtocol( "tag", "" );
client.Init( &e );
if ( e.Test() )
{
e.Fmt( &msg );
fprintf( "%s\n", msg.Text() );
return;
}
// Use my own client user.
CustomClientUser cu;
// Run the command. Try adding global options at the beginning of the arg array.
char * argv[] = { "-z", "tag", "-u", "td27117" };
int argc = sizeof( argv ) / sizeof( char * );
client.SetArgv( argc, argv );
client.Run( "clients", &cu );
But, that did not work.但是,那没有用。 The error output is what you would expect when you give it a command option it does not understand.
错误 output 是您在给它一个它不理解的命令选项时所期望的。
Usage: clients [ -t ] [ -u user ] [ -U ] [ [-e|-E] query -m max ] [ -a | -s serverID ] [ -S stream ]
Invalid option: -z.
The global options are arguments to the client app, not the server.全局选项是 arguments 到客户端应用程序,而不是服务器。 The
Run()
method is for sending commands/arguments to the server; Run()
方法用于向服务器发送命令/参数; if you send it the client-side args it won't know what to do with them.如果您将其发送到客户端参数,它将不知道如何处理它们。
To tell the client to set the "tag" protocol, do:要告诉客户端设置“标签”协议,请执行以下操作:
client.SetProtocol("tag", "");
Note that this must be called before you call Run()
since it modifies the protocol that is used by Run()
.请注意,这必须在调用
Run()
之前调用,因为它会修改Run()
使用的协议。
If you want to see how the various global options are implemented, you can just look at the p4
source code.如果你想看看各种全局选项是如何实现的,你可以看看
p4
源代码。
Note that most of the global options correspond directly to ClientApi
methods, which you can see in the header ( clientapi.h
) or in the documentation: https://www.perforce.com/manuals/v15.1/p4api/chapter.methods.html#clientapi请注意,大多数全局选项直接对应于
ClientApi
方法,您可以在 header ( clientapi.h
) 或文档中看到: https://www.perforce.com/manuals/v15.1/p4api/chapter。方法.html#clientapi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.