take_bytes
A lightweight Rust utility for reading input from stdin or a file path, designed for seamless integration with clap's derive API.
Features
- Simple API: Just parse
"-"for stdin or any path for file input - Clap Integration: Implements
FromStrfor direct use as a clap argument type - Zero Configuration: Works out of the box with sensible defaults
- Lightweight: Minimal dependencies
Installation
Add this to your Cargo.toml:
[]
= "0.1"
The clap feature is enabled by default. To use without clap:
[]
= { = "0.1", = false }
Usage
With Clap
use Parser;
use TakeBytes;
Programmatic Use
use TakeBytes;
use PathBuf;
// Read from stdin
let stdin_input = stdin;
// Read from a file
let file_input = file;
// Parse from string (like clap does)
let input: TakeBytes = "-".parse?;
Helper Functions
use ;
let input: TakeBytes = "file.txt".parse?;
// Read as raw bytes
let bytes = read_as_bytes?;
// Or decode as UTF-8 string
let input: TakeBytes = "file.txt".parse?;
let text = decode_utf8_string?;
Examples
The classic Unix pattern of accepting input from either a file or stdin:
# Read from a file
# Read from stdin
|
# Read from stdin (default)
|
License
MIT License - see LICENSE for details.