Use structopt.

This commit is contained in:
2025-04-13 10:20:43 -04:00
parent 7bf6c3299c
commit 41a9a2a3bf
3 changed files with 201 additions and 54 deletions

View File

@@ -1,14 +1,15 @@
// TODO: Header
use cxx_qt_lib::QString;
use std::env;
use std::error::Error;
use std::process::exit;
use structopt::StructOpt;
pub mod colors;
pub mod filesystem;
fn run_gui() -> Result<(), Box<dyn Error>> {
fn run_gui(root_path: std::path::PathBuf) -> Result<(), Box<dyn Error>> {
println!("Starting the GUI editor at {:?}", root_path);
use cxx_qt_lib::{QGuiApplication, QQmlApplicationEngine, QUrl};
let mut app = QGuiApplication::new();
@@ -28,53 +29,36 @@ fn run_gui() -> Result<(), Box<dyn Error>> {
Ok(())
}
fn run_tui() -> Result<(), Box<dyn Error>> {
println!("Starting TUI editor...");
// Your cursive logic here, like:
// let mut siv = cursive::default();
// siv.add_layer(...);
// siv.run();
fn run_tui(root_path: std::path::PathBuf) -> Result<(), Box<dyn Error>> {
println!("Starting the TUI editor at {:?}", root_path);
Ok(())
}
fn print_help() {
println!("Usage: app_launcher [OPTION]");
println!("Options:");
println!(" gui Launch the QML GUI application");
println!(" tui Launch the TUI text editor (cursive-based)");
println!(" help Show this help message");
#[derive(StructOpt, Debug)]
#[structopt(name = "clide")]
struct Cli {
/// The root path to open with the clide editor.
#[structopt(parse(from_os_str))]
pub path: Option<std::path::PathBuf>,
/// Run in headless mode if this flag is present.
#[structopt(name = "tui", short, long)]
pub tui: bool,
}
fn main() {
let args: Vec<String> = env::args().collect();
fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::from_args();
if args.len() < 2 {
if let Err(e) = run_gui() {
eprintln!("Error launching GUI: {}", e);
exit(1);
}
}
// If the CLI was provided a directory to open use it.
// Otherwise, attempt to find the home directory. If that fails use CWD.
let root_path = args
.path
.or_else(dirs::home_dir)
.unwrap_or_else(|| std::env::current_dir().expect("Failed to access filesystem."));
match args.get(1).map(|s| s.as_str()) {
Some("gui") => {
if let Err(e) = run_gui() {
eprintln!("Error launching GUI: {}", e);
exit(1);
}
}
Some("tui") => {
if let Err(e) = run_tui() {
eprintln!("Error launching TUI: {}", e);
exit(1);
}
}
Some("help") | None => {
print_help();
}
Some(arg) => {
eprintln!("Unknown argument: {}", arg);
print_help();
exit(1);
}
// Open the TUI editor if requested, otherwise use the QML GUI by default.
match args.tui {
true => run_tui(root_path),
false => run_gui(root_path),
}
}