pub struct FileOrStdout { /* private fields */ }Expand description
FileOrStdout can be used as a proxy output writer to write to whichever destination
was specified by the CLI args, a file or stdout.
use std::path::PathBuf;
use std::io::Write;
use clap::Parser;
use clap_stdin::FileOrStdout;
#[derive(Debug, Parser)]
struct Args {
output: FileOrStdout,
}
if let Ok(args) = Args::try_parse() {
let mut writer = args.output.into_writer()?;
write!(&mut writer, "1 2 3 4");
}$ ./example output.txt
1 2 3 4
$ cat output.txt | ./example -
1 2 3 4Implementations§
Source§impl FileOrStdout
impl FileOrStdout
Sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Was this value read from a file (path passed in from argument values)
Sourcepub fn filename(&self) -> &str
pub fn filename(&self) -> &str
The value passed to this arg (Either “-” for stdout or a filepath)
Sourcepub fn into_writer(self) -> Result<impl Write, Error>
pub fn into_writer(self) -> Result<impl Write, Error>
Create a writer for the dest, to allow user flexibility of how to write output (e.g. all at once or in chunks)
use std::io::Write;
use clap_stdin::FileOrStdout;
use clap::Parser;
#[derive(Parser)]
struct Args {
output: FileOrStdout,
}
let args = Args::parse();
let mut writer = args.output.into_writer()?;
let mut buf = vec![0;8];
writer.write_all(&mut buf)?;Trait Implementations§
Source§impl Clone for FileOrStdout
impl Clone for FileOrStdout
Source§fn clone(&self) -> FileOrStdout
fn clone(&self) -> FileOrStdout
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FileOrStdout
impl Debug for FileOrStdout
Auto Trait Implementations§
impl Freeze for FileOrStdout
impl RefUnwindSafe for FileOrStdout
impl Send for FileOrStdout
impl Sync for FileOrStdout
impl Unpin for FileOrStdout
impl UnwindSafe for FileOrStdout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more