CLI --export-svg Error Handling with pico-args
We resolved two issues when running turtle programs with `--export-svg`:
1. **Missing filename**: Previously, running `--export-svg` without a
filename caused the CLI parser to ignore the flag and open the GUI
window. Now, it emits `Error: --export-svg: option requires an
argument` and terminates with exit code 1.
2. **Missing `svg` feature**: Previously, running `--export-svg` without
the `svg` feature enabled ignored the flag if no filename was passed
and opened the window. Now, any invocation of `--export-svg` without
`--features svg` emits `Error: SVG export feature is not enabled.
Please rebuild with --features svg` and terminates with exit code 1.
---
- Added [`pico-args = { version = "0.5", features = ["eq-separator"]
}`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/Cargo.toml)
under `[dependencies]`.
- In
[`turtle-lib/src/export.rs`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib/src/export.rs):
- Added `MissingFeature(String)` to `ExportError` for clean error
display.
- Implemented `parse_svg_export_from_args(args: &mut
pico_args::Arguments) -> Result<Option<String>, pico_args::Error>`
to parse `--export-svg <file>` and `--export-svg=<file>`, treating
empty or flag arguments as missing values (`OptionWithoutAValue`).
- Updated `parse_svg_export_arg() -> Option<String>` for backward
compatibility.
- In `handle_svg_export`:
- Under `#[cfg(not(feature = "svg"))]`: detects any presence of
`--export-svg`, outputs `Error: SVG export feature is not enabled.
Please rebuild with --features svg`, and exits with code 1.
- Under `#[cfg(feature = "svg")]`: handles missing arguments with
`Error: --export-svg: option requires an argument` (exit 1), valid
exports (exit 0), and leaves unknown arguments untouched so
Macroquad or user flags continue to work.
- Added unit tests covering all flag parsing variants and error
displays.
- In
[`turtle-lib-macros/src/lib.rs`](file:///home/dietrich/Projekte/Source/turtlers/turtle-lib-macros/src/lib.rs):
- Simplified the macro expansion in `turtle_main` to delegate directly
to `turtle_lib::export::handle_svg_export(&mut build_commands);`.
- Updated documentation comments to match.
---
```bash
cargo test --package turtle-lib
cargo test --package turtle-lib --features svg
cargo test --package turtle-lib-macros
cargo clippy --package turtle-lib -- -Wclippy::pedantic
-Aclippy::cast_precision_loss -Aclippy::cast_sign_loss
-Aclippy::cast_possible_truncation
cargo clippy --package turtle-lib --features svg -- -Wclippy::pedantic
-Aclippy::cast_precision_loss -Aclippy::cast_sign_loss
-Aclippy::cast_possible_truncation
cargo clippy --package turtle-lib-macros -- -Wclippy::pedantic
```
- **Unit tests**: All 29 unit tests in `turtle-lib` and 18 unit tests in
`turtle-lib-macros` passed.
- **Doctests**: All 34 doctests passed.
- **Clippy**: Passed with zero warnings across all crates and feature
configurations.
| Scenario | Command | Result | Exit Code | Window Opened? |
|---|---|---|---|---|
| Missing feature, missing filename | `cargo run --example hello_turtle
-- --export-svg` | `Error: SVG export feature is not enabled. Please
rebuild with --features svg` | 1 | No |
| Missing feature, with filename | `cargo run --example hello_turtle --
--export-svg test.svg` | `Error: SVG export feature is not enabled.
Please rebuild with --features svg` | 1 | No |
| Feature enabled, missing filename | `cargo run --example hello_turtle
--features svg -- --export-svg` | `Error: --export-svg: option requires
an argument` | 1 | No |
| Feature enabled, with filename | `cargo run --example hello_turtle
--features svg -- --export-svg /tmp/pico_test.svg` | `SVG exported
successfully to: /tmp/pico_test.svg` | 0 | No |
| Feature enabled, equals syntax | `cargo run --example hello_turtle
--features svg -- --export-svg=/tmp/pico_test_eq.svg` | `SVG exported
successfully to: /tmp/pico_test_eq.svg` | 0 | No |
| Feature enabled, extra flags | `cargo run --example hello_turtle
--features svg -- --export-svg /tmp/pico_test_extra.svg --custom-flag` |
`SVG exported successfully to: /tmp/pico_test_extra.svg` | 0 | No |
| Normal run (no flag) | `cargo run --example hello_turtle` |
Interactive window opens normally | 0 | Yes |
This commit is contained in:
@@ -66,17 +66,11 @@ use syn::ItemFn;
|
||||
/// use turtle_lib::*;
|
||||
///
|
||||
/// fn main() {
|
||||
/// let mut build_commands = |turtle: &mut turtle_lib::TurtlePlan| {
|
||||
/// my_drawing(turtle);
|
||||
/// };
|
||||
/// // Handle optional SVG export headlessly without opening a window
|
||||
/// if let Some(filename) = turtle_lib::export::parse_svg_export_arg() {
|
||||
/// let mut build_commands = |turtle: &mut turtle_lib::TurtlePlan| {
|
||||
/// my_drawing(turtle);
|
||||
/// };
|
||||
/// if let Err(e) = turtle_lib::export::run_headless_svg_export(&mut build_commands, &filename) {
|
||||
/// eprintln!("Error exporting SVG: {:?}", e);
|
||||
/// std::process::exit(1);
|
||||
/// }
|
||||
/// return;
|
||||
/// }
|
||||
/// turtle_lib::export::handle_svg_export(&mut build_commands);
|
||||
///
|
||||
/// // Normal interactive GUI mode with window
|
||||
/// turtle_lib::macroquad::Window::new("My Turtle Drawing", async {
|
||||
@@ -252,18 +246,7 @@ fn turtle_main_impl(
|
||||
};
|
||||
|
||||
// If --export-svg flag is present, export headlessly without opening a window
|
||||
if let Some(filename) = turtle_lib::export::parse_svg_export_arg() {
|
||||
match turtle_lib::export::run_headless_svg_export(&mut build_commands, &filename) {
|
||||
Ok(()) => {
|
||||
println!("SVG exported successfully to: {}", filename);
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error exporting SVG: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
turtle_lib::export::handle_svg_export(&mut build_commands);
|
||||
|
||||
// Normal rendering mode (interactive window)
|
||||
turtle_lib::macroquad::Window::new(#window_title, async {
|
||||
|
||||
Reference in New Issue
Block a user