2 unstable releases

0.2.0 Sep 5, 2024
0.1.0 Jan 12, 2023

#9 in #cli-input

38 downloads per month

MIT license

11KB
231 lines

clap-io

Easily add a --input and --output flags to CLIs using clap

Example

cargo run --example copy -- --help

LICENSE

Copyright © 2023 Swift Navigation

Distributed under the MIT open source license.


lib.rs:

Add optional --input and --output flags to a clap command. If --input is not specified, it defaults to (locked) stdin. If --output is not specified, it defaults to (locked) stdout.

Examples

Add get --input and --output flags to your program:

use clap::Parser;
use clap_io::InputOutput;

#[derive(Parser)]
struct Cli {
    #[clap(flatten)]
    io: InputOutput,
}

let cli = Cli::parse();
let mut input = cli.io.input.open().unwrap();
let mut output = cli.io.output.open().unwrap();
std::io::copy(&mut input, &mut output).unwrap();

Add just one:

use clap::Parser;
use clap_io::Input;

#[derive(Parser)]
struct Cli {
   #[clap(long = "in")]
    input: Input,
}

let cli = Cli::parse();
eprintln!("is tty? {}", cli.input.is_tty());
eprintln!("path? {:?}", cli.input.path());

Dependencies

~1.2–2MB
~34K SLoC