-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,9 +39,17 @@ fn main() -> Result<()> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Note: Settings mode is no longer supported | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Settings are now opened via viewport mode from the main application | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // // Check if launched in settings mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let args: Vec<String> = std::env::args().collect(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // let is_settings_mode = args.iter().any(|arg| arg == "--settings"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 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."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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." | |
| ); |
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.
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()); | |
| } |
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()); | |
| } | |
| } |
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.