Skip to content

Commit

Permalink
clap 4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-ben committed Jul 30, 2024
1 parent 1c9ac76 commit 44a52ff
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
env_logger = "0.11"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
clap = "3.1"
clap = {version = "4.5", features = ["derive"] }
cgmath = "0.18"
rand = "0.8"
lerp = "0.5"
Expand Down
3 changes: 2 additions & 1 deletion crates/viewer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::*;
use serde::de::Unexpected;
use serde::Deserialize;
use std::fs::File;
use std::path::Path;
use vulkan::MsaaSamples;

#[derive(Deserialize, Clone)]
Expand Down Expand Up @@ -124,7 +125,7 @@ impl Default for Environment {
}
}

pub fn load_config(path: &str) -> Result<Config, AppError> {
pub fn load_config<P: AsRef<Path>>(path: P) -> Result<Config, AppError> {
let config_file = File::open(path)
.map_err(|e| AppError::ConfigLoadError(format!("Failed to load file: {}", e)))?;
serde_yaml::from_reader(config_file)
Expand Down
59 changes: 22 additions & 37 deletions crates/viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod loader;
mod renderer;

use crate::{camera::*, config::Config, controls::*, gui::Gui, loader::*, renderer::*};
use clap::{Arg, Command};
use clap::Parser;
use environment::*;
use model::{Model, PlaybackMode};
use std::{cell::RefCell, error::Error, path::PathBuf, rc::Rc, sync::Arc, time::Instant};
Expand All @@ -26,15 +26,18 @@ fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
log::info!("Welcome to gltf-viewer-rs");

let matches = create_app().get_matches();
let cli = Cli::parse();

let config = matches
.value_of("config")
.map_or(Ok(Default::default()), config::load_config)?;
let enable_debug = matches.is_present("debug");
let file_path = matches.value_of("file").map(PathBuf::from);
let config = cli
.config
.as_ref()
.map(config::load_config)
.transpose()?
.unwrap_or_default();
let enable_debug = cli.debug;
let model_path = cli.file;

run(config, enable_debug, file_path);
run(config, enable_debug, model_path);

Ok(())
}
Expand Down Expand Up @@ -236,33 +239,15 @@ fn run(config: Config, enable_debug: bool, path: Option<PathBuf>) {
.unwrap();
}

fn create_app<'a>() -> Command<'a> {
Command::new("GLTF Viewer")
.version("1.0")
.author("Adrien Bennadji")
.about("Viewer for GLTF 2.0 files.")
.arg(
Arg::new("config")
.short('c')
.long("config")
.value_name("FILE")
.help("Set the path to the configuration file")
.takes_value(true),
)
.arg(
Arg::new("file")
.short('f')
.long("file")
.value_name("FILE")
.help("Set the path to gltf model to view")
.takes_value(true),
)
.arg(
Arg::new("debug")
.short('d')
.long("debug")
.value_name("DEBUG")
.help("Enable vulkan debug printing")
.takes_value(false),
)
#[derive(Parser)]
#[command(name = "GLTF Viewer")]
#[command(version = "1.0")]
#[command(about = "Viewer for GLTF 2.0 files.", long_about = None)]
struct Cli {
#[arg(short, long, value_name = "FILE")]
config: Option<PathBuf>,
#[arg(short, long, value_name = "FILE")]
file: Option<PathBuf>,
#[arg(short, long)]
debug: bool,
}

0 comments on commit 44a52ff

Please sign in to comment.