Skip to content

Commit

Permalink
feat: pass data/map paths as cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascomj committed Jan 20, 2025
1 parent 8e4e49f commit ac4fa82
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
56 changes: 56 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Module that handles CLI to supply input files as arguments to the executable.
use bevy::prelude::{App, Entity, FileDragAndDrop};
use bevy::window::PrimaryWindow;
use std::env;
use std::path::PathBuf;

pub struct CliArgs {
pub map_path: Option<PathBuf>,
pub data_path: Option<PathBuf>,
}

pub fn parse_args() -> CliArgs {
let args: Vec<String> = env::args().collect();
// the last args take priority
let (map_path, data_path) = args.iter().skip(1).zip(args.iter().skip(2)).fold(
(None, None),
|(map, data), (arg, next)| match arg.as_str() {
"--map" => (Some(PathBuf::from(next)), data),
"--data" => (map, Some(PathBuf::from(next))),
_ => (map, data),
},
);

CliArgs {
map_path,
data_path,
}
}

/// Generate `FileDragAndDrop` such that the map and/or data
/// if supplied as CLI args are later loaded.
pub fn handle_cli_args(app: &mut App, cli_args: CliArgs) {
let Some((win, _)) = app
.world_mut()
.query::<(Entity, &PrimaryWindow)>()
.iter(app.world())
.next()
else {
return;
};
// paths are canonicalized so that they are not interpreted
// to be in the assets directory by bevy's `AssetLoader`.
if let Some(map_path) = cli_args.map_path {
app.world_mut().send_event(FileDragAndDrop::DroppedFile {
window: win,
path_buf: map_path.canonicalize().unwrap(),
});
}

if let Some(data_path) = cli_args.data_path {
app.world_mut().send_event(FileDragAndDrop::DroppedFile {
window: win,
path_buf: data_path.canonicalize().unwrap(),
});
}
}
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use bevy_pancam::{PanCam, PanCamPlugin};
use bevy_prototype_lyon::prelude::*;

mod aesthetics;
#[cfg(not(target_arch = "wasm32"))]
mod cli;
mod data;
mod escher;
mod funcplot;
Expand All @@ -23,7 +25,8 @@ use screenshot::{RawAsset, RawFontStorage};

#[cfg(not(target_arch = "wasm32"))]
fn main() {
App::new()
let mut app = App::new();
let app = app
.insert_resource(WinitSettings::desktop_app())
.add_plugins(
DefaultPlugins
Expand All @@ -47,8 +50,12 @@ fn main() {
.add_plugins(data::DataPlugin)
.add_systems(Startup, setup_system)
.add_plugins(aesthetics::AesPlugin)
.add_plugins(legend::LegendPlugin)
.run();
.add_plugins(legend::LegendPlugin);

let cli_args = cli::parse_args();
cli::handle_cli_args(app, cli_args);

app.run();
}

#[cfg(target_arch = "wasm32")]
Expand Down

0 comments on commit ac4fa82

Please sign in to comment.