Crate clio

source · []
Expand description

clio is a library for parsing CLI file names.

It implemts the standard unix convetions of when the file name is “-” then sending the data to stdin/stdout as apropriate

Usage

Inputs and Outputs can be created directly from the args The will error if the file cannot be opened for any reason

// a cat replacement
fn main() -> clio::Result<()> {
    for arg in std::env::args_os() {
        let mut input = clio::Input::new(&arg)?;
        std::io::copy(&mut input, &mut std::io::stdout())?;
    }
    Ok(())
}

They are also desgined to be used with structopt/clap

use clap::Parser;
use clio::*;

#[derive(Parser)]
#[clap(name = "cat")]
struct Opt {
    /// Input file, use '-' for stdin
    #[clap(parse(try_from_os_str = TryFrom::try_from), default_value="-")]
    input: Input,

    /// Output file '-' for stdout
    #[clap(long, short, parse(try_from_os_str = TryFrom::try_from), default_value="-")]
    output: Output,
}

fn main() {
    let mut opt = Opt::parse();

    std::io::copy(&mut opt.input, &mut opt.output).unwrap();
}

Features

http

bundles in ureq as a HTTP client.

If a url is passed to Input::new then it will perform and HTTP GET.

If a url is passed to Output::new then it will perform and HTTP PUT. You can use SizedOutput to set the size before the upload starts e.g. needed if you are sending a file to S3.

Structs

A struct that contains all the connents of a command line input stream, either std in or a file

Enums

Any error that happens when opening a stream.

An enum that represents a command line input stream, either Stdin or File

An enum that represents a command line output stream, either Stdout or a File along with it’s path

A builder for Output that allows setting the size before writing. This is mostly usefull with the “http” feature for setting the Content-Length header

Type Definitions

A result with a clio::Error