input_py
input_py is a simple Rust library that provides Python-like input functionality for reading user input from the terminal.
Features
- 🐍 Python-like syntax: Familiar
input()function similar to Python - 🛡️ Robust error handling: Proper error types instead of panics
- 🎯 Default values: Support for default values when no input is provided
- ✂️ Configurable trimming: Control whitespace handling behavior
- 📝 Rich documentation: Comprehensive examples and documentation
- ✅ Well tested: Extensive test suite with 14+ tests
Installation
Add this to your Cargo.toml:
[]
= "1.0.0"
Quick Start
use input;
Terminal output:
Enter your name: Alice
Hello, Alice!
API Reference
input(comment: &str)
Basic input function that prompts the user and returns the trimmed input.
use input;
// Basic usage
let name = input?;
println!;
// Empty prompt
let data = input?; // No prompt displayed
input_with_default(comment: &str, default: &str)
Input with a default value that's used when the user enters nothing.
use input_with_default;
// With default value
let port = input_with_default?;
println!;
// User sees: "Enter port [8080]:"
// Pressing Enter uses "8080"
input_trim(comment: &str, trim_whitespace: bool)
Input with configurable whitespace trimming behavior.
use input_trim;
// Preserve whitespace
let raw_text = input_trim?;
println!;
// Trim whitespace (default behavior)
let clean_text = input_trim?;
println!;
Error Handling
All functions return Result<String, InputError> for robust error handling:
use ;
match input
Complete Example
use ;
Testing
Run the test suite:
The library includes comprehensive tests covering:
- ✅ Normal input scenarios
- ❌ Error conditions
- 🔧 Internal logic verification
- 🎯 Edge cases and special characters
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
v1.0.0 🎉
- 🚀 Stable release - Ready for production use
- ✨ Added
input_with_default()function for default value support - ✨ Added
input_trim()function for configurable whitespace handling - 🛡️ Improved error handling with custom
InputErrortype (breaking change) - 📚 Enhanced documentation with comprehensive examples and API reference
- ✅ Added extensive test suite (14+ tests) with full coverage
- 🔧 Fixed clippy warnings for better code quality
- 📖 Complete README with examples, comparison table, and usage guide
v0.2.1
- Basic input functionality similar to Python's input()