Struct clio::CachedInput
source · pub struct CachedInput { /* private fields */ }
Expand description
A struct that contains all the components of 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::CachedInput;
#[derive(Parser)]
struct Opt {
/// path to file, use '-' for stdin
#[clap(value_parser)]
input_file: CachedInput,
}
Implementations§
source§impl CachedInput
impl CachedInput
sourcepub fn new<S: TryInto<ClioPath>>(path: S) -> Result<Self>
pub fn new<S: TryInto<ClioPath>>(path: S) -> Result<Self>
Reads all the data from an file (stdin for “-”) into memory and stores it in a new CachedInput. If it detects it is trying to read from a TTY then it will return an error.
Useful if you want to use the input twice (see reset), or need to know the size.
This is mostly a wrapper around Input::read_all()
so so that any errors
reading the data will be shown automatically with claps pretty error formatting.
sourcepub fn std() -> Result<Self>
pub fn std() -> Result<Self>
Reads all the data from stdin into memory and stores it in a new CachedInput.
This will block until std in is closed.
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 CachedInput
either by opening the file or for ‘-’ stdin and reading
all the data into memory.
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) -> u64
pub fn len(&self) -> u64
Returns the size of the file in bytes.
Examples
let file = clio::CachedInput::try_from_os_str("foo.txt".as_ref()).unwrap();
assert_eq!(3, file.len());
Trait Implementations§
source§impl BufRead for CachedInput
impl BufRead for CachedInput
source§fn fill_buf(&mut self) -> IoResult<&[u8]>
fn fill_buf(&mut self) -> IoResult<&[u8]>
source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moresource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left
)Read
has any data left to be read. Read moresource§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
bufread_skip_until
)byte
or EOF is reached. Read more1.0.0 · source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided String
buffer. Read moresource§impl Clone for CachedInput
impl Clone for CachedInput
source§impl Debug for CachedInput
impl Debug for CachedInput
source§impl Display for CachedInput
impl Display for CachedInput
formats as the path it was created from
source§impl Read for CachedInput
impl Read for CachedInput
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 CachedInput
impl Seek for CachedInput
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
)source§impl TryFrom<&OsStr> for CachedInput
impl TryFrom<&OsStr> for CachedInput
source§impl TryFrom<&OsString> for CachedInput
impl TryFrom<&OsString> for CachedInput
source§impl TryFrom<&Path> for CachedInput
impl TryFrom<&Path> for CachedInput
source§impl TryFrom<&PathBuf> for CachedInput
impl TryFrom<&PathBuf> for CachedInput
source§impl TryFrom<&String> for CachedInput
impl TryFrom<&String> for CachedInput
source§impl TryFrom<&str> for CachedInput
impl TryFrom<&str> for CachedInput
source§impl TryFrom<ClioPath> for CachedInput
impl TryFrom<ClioPath> for CachedInput
source§impl ValueParserFactory for CachedInput
Available on crate feature clap-parse
only.
impl ValueParserFactory for CachedInput
clap-parse
only.§type Parser = OsStrParser<CachedInput>
type Parser = OsStrParser<CachedInput>
ValueParser
]. Read moresource§fn value_parser() -> Self::Parser
fn value_parser() -> Self::Parser
Self::Parser
]