6 releases (3 breaking)

0.4.1 Mar 24, 2020
0.4.0 Sep 26, 2017
0.3.1 Apr 6, 2017
0.3.0 Feb 12, 2017
0.1.0 Nov 19, 2016

#9 in #file-input

Download history 20/week @ 2023-12-06 31/week @ 2023-12-13 35/week @ 2023-12-20 13/week @ 2023-12-27 15/week @ 2024-01-03 37/week @ 2024-01-10 21/week @ 2024-01-17 7/week @ 2024-01-24 8/week @ 2024-01-31 31/week @ 2024-02-07 42/week @ 2024-02-14 47/week @ 2024-02-21 72/week @ 2024-02-28 59/week @ 2024-03-06 69/week @ 2024-03-13 51/week @ 2024-03-20

259 downloads per month
Used in 12 crates

Apache-2.0

7KB
100 lines

Introduction

A pattern that often occurs in UNIX utilities is:

  • You want to read from a file when a filename argument is provided, otherwise from stdin.
  • You want to write to a file when a filename argument is provided, otherwise to stdout.

This is a small crate that accommodates that pattern.

Note: This package is still new, its API will change.

Installation

This package can be used with Cargo:

[dependencies]
stdinout = 0.1

lib.rs:

A pattern that often occurs when writing command-line utilities is that one wants to open a file when a filename argument is provided or read/write from/to stdin/stdout otherwise. Unfortunatlely, this is more work in Rust than it should be.

The stdinout crate provides a small wrapper that makes it easier to handle this scenario.

For reading from a file or the standard input:

let input = Input::from(matches.free.get(0));
let reader = or_exit(input.buf_read());

for line in reader.lines() {
    // Use 'line'
}

For writing to a file or the standard output:

let output = Output::from(args.get(1));

// Get an object that implements the Write trait.
let write = output.write().unwrap();

No runtime deps