pub enum IoArg {
StdStream,
File(PathBuf),
}
Expand description
Argument for CLI tools which can either take a file or STDIN/STDOUT.
Caveat: stdin is represented as “-” at the command line. Which means your tool is going to have a hard time operating on a file named “-”.
use clap::Parser;
use io_arg::IoArg;
#[derive(Debug, Parser)]
struct Cli {
/// Path to input file. Set to "-" to use STDIN instead of a file.
input: IoArg,
/// Path to output file. Leave out or set to "-" to use STDOUT instead of a file.
output: IoArg,
}
Variants§
StdStream
Indicates that the IO is connected to stdin/stdout. Represented as a “-” on the command line.
File(PathBuf)
Indicates that the IO is connected to a file. Contains the file path. Just enter a path at the command line.
Implementations§
Source§impl IoArg
impl IoArg
Sourcepub fn is_file(&self) -> bool
pub fn is_file(&self) -> bool
Intended for use in an if
expression or other situations there a boolean might be more
convinient than matching against variants.
§Return
true
if variant is File
.
false
if variant is StdStream
.
Sourcepub fn open_as_input(self) -> Result<Input>
pub fn open_as_input(self) -> Result<Input>
Either calls stdin
or File::open
depending on io_arg
.
Sourcepub fn open_as_output(self) -> Result<Output>
pub fn open_as_output(self) -> Result<Output>
Either calls stdout
or File::create
depending on io_arg
.
Trait Implementations§
impl Eq for IoArg
impl StructuralPartialEq for IoArg
Auto Trait Implementations§
impl Freeze for IoArg
impl RefUnwindSafe for IoArg
impl Send for IoArg
impl Sync for IoArg
impl Unpin for IoArg
impl UnwindSafe for IoArg
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