Send a command

Send commands using P4API.NET methods

To send commands to the Helix Core Server, your client can use P4API.NET methods.

p4 changes command

// set the options for the p4 changes command
string clientName = "admin_space";
int maxItems = 5;
string userName = "admin";
ChangesCmdOptions options = new ChangesCmdOptions(ChangesCmdFlags.LongDescription,
	clientName, maxItems, ChangeListStatus.None, userName);


// run the command against the current repository
IList<Changelist> changes = rep.getChangelists(options, null);


// changes will contain the data returned by the command
// p4 changes -L -c admin_space -m 5 -u admin

p4 edit command

// set the options for the p4 edit command
string depotPath = "//depot/main/test.txt";
int changelist = 0;
// create FileSpec with null for ClientPath, LocalPath, and VersionSpec
FileSpec fileToCheckout = new FileSpec(new DepotPath(depotPath), null, null, null)
// using null for FileType
EditCmdOptions options = new EditCmdOptions(EditFilesCmdFlags.None, changelist, null);


// run the command with the current connection
IList<FileSpec> filesCheckedOut = con.Client.EditFiles(options, fileToCheckout);


// filesCheckedOut will contain the data returned by the command
// p4 edit //depot/main/test.txt
// and the file will be checked out in the default pending changelist

p4 submit command

// set the options for the p4 submit command
// 0 to specify default pending changelist, null for changelist since we are using the default
string description = "update to test.txt";
// set reopen to false
ClientSubmitOptions clientOptions = new ClientSubmitOptions(false,SubmitType.SubmitUnchanged)
SubmitCmdOptions options = new SubmitCmdOptions(SubmitFilesCmdFlags.None,
		     0, null, description, clientOptions)
// create FileSpec with null for ClientPath, LocalPath, and VersionSpec
string depotPath = "//depot/main/test.txt";
FileSpec fileToSubmit = new FileSpec(new DepotPath(depotPath), null, null, null)


// run the command with the current connection
SubmitResults results= con.Client.SubmitFiles(options, fileToSubmit);


// results will contain the data returned by the command
// p4 submit -d "update to test.txt" //depot/main/test.txt

Run commands directly

To run commands that do not have methods in the .NET API, your client can use the P4Command.Run() method.

Running command methods is more useful when you care about the results of the command and want them parsed for display or to be passed on to other commands. If command results are not important, running the command directly may be preferable.

For example:

// create a new command using parameters:
// repository, command, tagged, arguments
string file="//depot/main/test.txt";
P4Command cmd = new P4Command(rep, "attribute", true, file);
Options opts = new Options();
opts["-n"] = "fileID";
opts["-v"] = "1";


//run command and get results
P4CommandResult results = cmd.Run(opts);


// results will contain the data returned by the command
// p4 -ztag attribute -n fileID -v 1 //depot/main/test.txt

Working with Options

Options have subclasses specific to particular commands. These dictionary objects can also be created directly, for example:

ClientCmdOptions clientOpts = new ClientCmdOptions(ClientCmdFlags.Output);

will create the same options as:

Options opts = new Options();
opts["-o"]=null;

A command run with either of these options will use the -o flag. Refer to the Options Constructors for details on specific options flags.

Note

Some option flags are mutually exclusive and using them together will result in the same errors that would be returned from the command line.

Examples of correct usage:

// the SubmitCmdOptions class
public SubmitCmdOptions(SubmitFilesCmdFlags flags, int changelist, Changelist newChangelist,
string description, ClientSubmitOptions submitOptions)

// options for submitting from the default changelist
// the SubmitCmdOptions and ClientSubmitOptions can vary
SubmitCmdOptions submitOpts = new SubmitCmdOptions(SubmitFilesCmdFlags.None, 0, null, "submitting
from default changelist", new ClientSubmitOptions(false,SubmitType.SubmitUnchanged))

// options for submitting from a numbered changelist 16
// the SubmitCmdOptions and ClientSubmitOptions can vary

SubmitCmdOptions submitOpts = new SubmitCmdOptions(SubmitFilesCmdFlags.IncludeJobs, 16, null,
null, new ClientSubmitOptions(false,SubmitType.RevertUnchanged));

// options for submitting a new changelist using a changelist specification where change
// is a P4.Changelist
// the SubmitCmdOptions and ClientSubmitOptions can vary
SubmitCmdOptions submitOpts = new SubmitCmdOptions(SubmitFilesCmdFlags.ReopenFiles, 0, change,
null, new ClientSubmitOptions(true,SubmitType.SubmitUnchanged));
 
// options for submitting a shelved file from a numbered changelist 18
// the SubmitCmdOptions and ClientSubmitOptions can vary
SubmitCmdOptions submitOpts = new SubmitCmdOptions(Perforce.P4.SubmitFilesCmdFlags.SubmitShelved,
18, null, null, null);

Example of incorrect usage

Here is an example of improper usage of flags that are mutually exclusive:

// the SubmitCmdOptions class
public SubmitCmdOptions(SubmitFilesCmdFlags flags, int changelist, Changelist newChangelist,
string description, ClientSubmitOptions submitOptions)

// improper options for submitting from a numbered changelist 20
// the SubmitCmdOptions and ClientSubmitOptions can vary
SubmitCmdOptions submitOpts = new SubmitCmdOptions(SubmitFilesCmdFlags.None, 16, null,
"submitting change 20", new ClientSubmitOptions(false,SubmitType.RevertUnchanged));

Build a FileSpec

Commands for working with files and paths will need a FileSpec or a list or an array of FileSpecs as an argument when running the command. A FileSpec consists of a PathSpec (or PathSpecs) and a VersionSpec. The PathSpecs can be DepotPaths, ClientPaths, and LocalPaths. The VersionSpec can be a revision or a revision range. Refer to the VersionSpec class for additional revision types.

Example FileSpecs:

// create a FileSpec for //depot/test.txt
// using new FileSpec(PathSpec path, VersionSpec version)
PathSpec path = new DepotPath("//depot/test.txt");
FileSpec depotFile = new FileSpec(path, null);

// create a FileSpec for //depot/test.txt#5
// using new FileSpec(PathSpec path, VersionSpec version)
path = new DepotPath("//depot/test.txt");
VersionSpec version = new Revision(5);
depotFile = new FileSpec(path, version);

// create a FileSpec for //depot/test.txt@16,@22
// (version range between changelists 16 and 22)
// using new FileSpec(PathSpec path, VersionSpec version)
path = new DepotPath("//depot/test.txt");
VersionSpec lowerChangeID = new ChangelistIdVersion(16);
VersionSpec upperChangeID = new ChangelistIdVersion(22);
version = new VersionRange(lowerChangeID,upperChangeID);
depotFile = new FileSpec(path, version);

// create a FileSpec for C:\Users\username\Depot\test.txt#head
// using new FileSpec(PathSpec path, VersionSpec version)
path = new LocalPath("C:\\Users\\username\\Depot\\test.txt");
version = new HeadRevision();
depotFile = new FileSpec(path, version);

// create a FileSpec for //depot/test.txt@labelName
// using new FileSpec(PathSpec path, VersionSpec version)
path = new DepotPath("//depot/test.txt");
version = new LabelNameVersion("labelName");
depotFile = new FileSpec(path, version);

// create a FileSpec for //depot/...@2013/5/27,@now
// (version range between 2013/5/27 and now)
// using new FileSpec(PathSpec path, VersionSpec version)
path = new DepotPath("//depot/...");
DateTimeVersion lowerTimeStamp = new DateTimeVersion(new DateTime(2013,5,27));
DateTimeVersion upperTimeStamp = new DateTimeVersion(DateTime.Now);
version = new VersionRange(lowerTimeStamp,upperTimeStamp);
depotFile = new FileSpec(path, version);;