#input-output #clap #flags #cli #command #stdin #stdout

clap-io

Add input and output flags to clap commands

1 unstable release

0.1.0 Jan 12, 2023

#65 in #input-output

Download history 6/week @ 2024-02-09 90/week @ 2024-02-16 19/week @ 2024-02-23 12/week @ 2024-03-01 22/week @ 2024-03-08 11/week @ 2024-03-15 25/week @ 2024-03-22 35/week @ 2024-03-29 11/week @ 2024-04-05

90 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
~35K SLoC