-
Notifications
You must be signed in to change notification settings - Fork 2
Add CLI to open file #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds command-line interface support to allow users to specify an initial file to open when launching Thoth from the terminal. The implementation adds basic CLI argument parsing for a --help flag and an optional file path argument.
Changes:
- Added CLI argument parsing in main.rs to handle
--help/-hflags and accept an optional file path - Modified
ThothApp::new()to accept an optional initial file path parameter - Configured the window state to set the initial file path, which triggers automatic loading by the CentralPanel
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/main.rs | Added CLI argument parsing logic to capture help flag and optional file path, then pass it to ThothApp |
| src/app/thoth_app.rs | Updated constructor signature to accept optional initial file and set it in window state for automatic loading |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for arg in std::env::args_os().skip(1) { | ||
| if arg == std::ffi::OsString::from("--help") || arg == std::ffi::OsString::from("-h") { | ||
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); | ||
| return Ok(()); | ||
| } | ||
| // First non-flag arg is treated as file path | ||
| cli_file = Some(std::path::PathBuf::from(arg)); | ||
| break; | ||
| } | ||
|
|
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI argument parsing logic has a flaw: if multiple arguments are provided, only the first non-flag argument is captured as the file path, but any subsequent arguments are silently ignored without validation. This could confuse users who pass multiple file paths expecting them all to be opened or rejected with an error. Consider adding validation to detect and warn about unexpected additional arguments.
| for arg in std::env::args_os().skip(1) { | |
| if arg == std::ffi::OsString::from("--help") || arg == std::ffi::OsString::from("-h") { | |
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); | |
| return Ok(()); | |
| } | |
| // First non-flag arg is treated as file path | |
| cli_file = Some(std::path::PathBuf::from(arg)); | |
| break; | |
| } | |
| let mut extra_args: Vec<std::ffi::OsString> = Vec::new(); | |
| for arg in std::env::args_os().skip(1) { | |
| if arg == std::ffi::OsString::from("--help") || arg == std::ffi::OsString::from("-h") { | |
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); | |
| return Ok(()); | |
| } | |
| // First non-flag arg is treated as file path; any additional args are rejected | |
| if cli_file.is_none() { | |
| cli_file = Some(std::path::PathBuf::from(&arg)); | |
| } else { | |
| extra_args.push(arg); | |
| } | |
| } | |
| if !extra_args.is_empty() { | |
| eprintln!("Error: unexpected additional argument(s): {:?}", extra_args); | |
| eprintln!( | |
| "Usage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup." | |
| ); | |
| return Err("Unexpected additional CLI arguments".into()); | |
| } |
| let mut cli_file: Option<std::path::PathBuf> = None; | ||
| for arg in std::env::args_os().skip(1) { | ||
| if arg == std::ffi::OsString::from("--help") || arg == std::ffi::OsString::from("-h") { | ||
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The help message format is inconsistent with standard CLI conventions. The entire message is on a single line making it harder to read and maintain. Additionally, the em dash character (—) may not render properly in all terminal environments. Consider breaking this into multiple lines and using a regular hyphen for better compatibility.
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); | |
| println!( | |
| "Thoth - JSON & NDJSON Viewer\n\ | |
| \n\ | |
| Usage: thoth [OPTIONS] [FILE]\n\ | |
| \n\ | |
| Options:\n\ | |
| -h, --help Show this help message\n\ | |
| \n\ | |
| If a FILE is supplied, Thoth will open it on startup." | |
| ); |
| cli_file = Some(std::path::PathBuf::from(arg)); | ||
| break; | ||
| } | ||
|
|
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI parsing does not validate whether the provided file path exists or is accessible before passing it to the application. While the CentralPanel will eventually handle file opening errors, early validation here would provide a better user experience by giving immediate feedback if the file doesn't exist, rather than launching the GUI only to show an error. Consider adding a basic existence check and providing a meaningful error message on the command line.
| // If a CLI file was provided, validate that it exists before launching the GUI | |
| if let Some(ref path) = cli_file { | |
| if !path.exists() { | |
| eprintln!( | |
| "Error: File '{}' does not exist or is not accessible.", | |
| path.to_string_lossy() | |
| ); | |
| return Err("CLI file path does not exist".into()); | |
| } | |
| } |
| // Parse simple CLI flags: --help / -h and optional FILE arg | ||
| let mut cli_file: Option<std::path::PathBuf> = None; | ||
| for arg in std::env::args_os().skip(1) { | ||
| if arg == std::ffi::OsString::from("--help") || arg == std::ffi::OsString::from("-h") { | ||
| println!("Thoth — JSON & NDJSON Viewer\n\nUsage: thoth [OPTIONS] [FILE]\n\nOptions:\n -h, --help Show this help message\n\nIf a FILE is supplied, Thoth will open it on startup."); | ||
| return Ok(()); | ||
| } | ||
| // First non-flag arg is treated as file path | ||
| cli_file = Some(std::path::PathBuf::from(arg)); | ||
| break; | ||
| } |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CLI file path argument lacks test coverage. Given that the codebase has comprehensive tests for other components, this new CLI functionality should have corresponding tests to verify proper argument parsing, help message output, and file path handling. Consider adding integration tests that validate the CLI argument parsing behavior.
| // If an initial file was provided via CLI, set it so central panel will attempt to open it on first render | ||
| if let Some(path) = initial_file { | ||
| window_state.file_path = Some(path); | ||
| window_state.error = None; | ||
| } |
Copilot
AI
Jan 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The initial file handling logic directly sets the file_path without considering whether file_type needs to be determined. While the CentralPanel's file opening logic handles file type detection (lines 75-93 in central_panel.rs), it might be more explicit to document this dependency or ensure file_type is left as default when setting an initial file path. The current implementation relies on implicit default behavior which may not be immediately clear to future maintainers.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59 +/- ##
==========================================
- Coverage 26.87% 26.82% -0.05%
==========================================
Files 58 58
Lines 5016 5025 +9
==========================================
Hits 1348 1348
- Misses 3668 3677 +9 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Adds support for specifying an initial file when launching the app from the terminal