Expand description
An enum that represents a command line input stream, either std in or a file
It is designed to be used with the clap
crate when taking a file name as an
argument to CLI app
use clap::Parser;
use clio::Input;
#[derive(Parser)]
struct Opt {
/// path to file, use '-' for stdin
#[clap(parse(try_from_os_str = TryFrom::try_from))]
input_file: Input,
}
Variants
Stdin(Stdin)
Pipe(OsString, File)
File(OsString, File)
Implementations
sourceimpl Input
impl Input
sourcepub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self>
pub fn new<S: AsRef<OsStr>>(path: S) -> Result<Self>
Contructs a new input either by opening the file or for ‘-’ returning stdin
sourcepub fn try_from_os_str(path: &OsStr) -> Result<Self, OsString>
pub fn try_from_os_str(path: &OsStr) -> Result<Self, OsString>
Contructs a new input either by opening the file or for ‘-’ returning stdin
The error is converted to a OsString
so that stuctopt can show it to the user.
It is recomended that you use TryFrom::try_from
and clap 3.0 instead.
sourcepub fn len(&self) -> Option<u64>
pub fn len(&self) -> Option<u64>
If input is a file, returns the size of the file, in bytes otherwise if input is stdin returns none.
Examples
let file = clio::Input::new("foo.txt").unwrap();
assert_eq!(Some(3), file.len());
sourcepub fn is_empty(&self) -> Option<bool>
pub fn is_empty(&self) -> Option<bool>
Returns a boolean saying if the file is empty, if using stdin returns None
Examples
let file = clio::Input::new("foo.txt").unwrap();
assert_eq!(Some(true), file.is_empty());
Trait Implementations
sourceimpl Read for Input
impl Read for Input
sourcefn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
sourceimpl Seek for Input
impl Seek for Input
sourcefn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
fn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
Seek to an offset, in bytes, in a stream. Read more
1.55.0 · sourcefn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
Auto Trait Implementations
impl RefUnwindSafe for Input
impl Send for Input
impl Sync for Input
impl Unpin for Input
impl UnwindSafe for Input
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more