Skip to main content

print/
print.rs

1//! Example of the `p4 print` command using the builder pattern.
2//!
3//! Builder methods consume and return `Self`, so options can be chained
4//! fluently in a single expression. Run with:
5//!
6//! ```text
7//! cargo run --example print
8//! ```
9
10use perforce_cli::P4Cli;
11use perforce_cli::spawn::{ParameterizedOutput, ParameterizedSpawn};
12use std::ffi::OsStr;
13use std::path::Path;
14
15fn main() -> std::io::Result<()> {
16    let p4 = P4Cli::default();
17
18    // Chain builders: print all revisions of the first two matching files to
19    // a local output file, without the depot header line.
20    let mut print = p4
21        .print()
22        .all_revisions(true)
23        .quiet_mode(true)
24        .limit(2)
25        .redirect_output("print-output.txt");
26
27    // Run the command to completion and capture its output.
28    let file = Path::new("//depot/project/README.md");
29    let output = print.output_with(&[file.as_os_str()])?;
30    println!("{}", String::from_utf8_lossy(&output.stdout));
31
32    // Or stream the contents directly to the terminal with `spawn_with`.
33    let mut child = p4
34        .print()
35        .spawn_with(&[OsStr::new("//depot/project/README.md")])?;
36    child.wait()?;
37
38    Ok(())
39}