pub struct Input { /* private fields */ }
Expand description
An enum that represents a command line input stream,
either Stdin
or 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(value_parser)]
input_file: Input,
}
Implementations§
source§impl Input
impl Input
sourcepub fn new<S: TryInto<ClioPath>>(path: S) -> Result<Self>
pub fn new<S: TryInto<ClioPath>>(path: S) -> Result<Self>
Constructs 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>
Constructs 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 recommended 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 get_file(&mut self) -> Option<&mut File>
pub fn get_file(&mut self) -> Option<&mut File>
If input is a file, returns a reference to the file, otherwise if input is stdin or a pipe returns none.
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§
source§impl Clone for Input
Available on crate feature clap-parse
only.
impl Clone for Input
clap-parse
only.Opens a new handle on the file from the path that was used to create it Probably a bad idea to have two write handles to the same file or to std in There is no effort done to make the clone be at the same position as the original
This will panic if the file has been deleted
Only included when using the clap-parse
feature as it is needed for value_parser
source§impl Read for Input
impl Read for Input
source§fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moresource§impl Seek for Input
impl Seek for Input
source§fn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
fn seek(&mut self, pos: SeekFrom) -> IoResult<u64>
1.55.0 · source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len
)