Crate clio

source ·
Expand description

clio is a library for parsing CLI file names.

It implements 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 args in args_os. They 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(())
}

With the clap-parse feature they are also desgined to be used with clap 3.2.

See the older docs for examples of older clap/structopt

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

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

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

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

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

Features

clap-parse

Implements ValueParserFactory for all the types and adds a bad implmentation of Clone to all types as well to keep clap happy.

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 OutputPath::create_with_len to set the size before the upload starts e.g. needed if you are sending a file to S3.

http-ureq

bundles in ureq as a HTTP client.

http-curl

bundles in curl as a HTTP client.

Modules

  • implementation of TypedValueParser for clio types so that they can be used with clap value_parser

Structs

  • A struct that contains all the connents of a command line input stream, either std in or a file
  • A builder for Input and Output.
  • An enum that represents a command line input stream, either Stdin or File
  • A builder for Input that validates the path but defers creating it until you call the open method.
  • A struct that represents a command line output stream, either Stdout or a File along with it’s path
  • A builder for Output that validates the path but defers creating it until you call the create method.

Enums

  • Any error that happens when opening a stream.

Type Definitions