Skip to content

Commit 5db56b9

Browse files
authored
Configure catalog and search paths via CLI args (#15)
Add some command-line options to configure the search paths of files and sub-files: - `-C`/`--catalog-path` (required) : sets the catalog path where files and sub-files are searched for. - `-I`/`--include-path` (optional) : add a single search path in addition of the catalog path. This option can be repeated multiple times, once per additional path.
1 parent efaa14a commit 5db56b9

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

bin/weldr/src/weldr.rs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod gltf;
88

99
use error::Error;
1010

11-
use ansi_term::Color::Red;
11+
use ansi_term::Color::{Red, Yellow};
1212
use ordered_float::NotNan;
1313
use std::{
1414
collections::HashMap,
@@ -50,6 +50,30 @@ struct ConvertCommand {
5050
/// Output line segments in addition of surfaces (triangles/quads).
5151
#[structopt(long = "lines", short = "l", takes_value = false)]
5252
with_lines: bool,
53+
54+
/// Add the given location to the list of search paths for resolving parts.
55+
///
56+
/// The location is appended to the list of search paths, in addition of the canonical paths derived
57+
/// from the catalog location. This option can be used multiple times to add more search paths.
58+
#[structopt(
59+
long = "include-path",
60+
short = "I",
61+
value_name = "PATH",
62+
// multiple=true + number_of_values=1 => can appear multiple times, with one value per occurrence
63+
multiple = true,
64+
number_of_values = 1
65+
)]
66+
include_paths: Option<Vec<PathBuf>>,
67+
68+
/// Set the root location of the official LDraw catalog for resolving parts.
69+
///
70+
/// This option adds the following search paths to the list of paths to resolve sub-file references from:
71+
/// - <PATH>/p
72+
/// - <PATH>/p/48
73+
/// - <PATH>/parts
74+
/// - <PATH>/parts/s
75+
#[structopt(long = "catalog-path", short = "C", value_name = "PATH")]
76+
catalog_path: Option<PathBuf>,
5377
}
5478

5579
#[derive(StructOpt)]
@@ -92,8 +116,8 @@ impl DiskResolver {
92116
DiskResolver { base_paths: vec![] }
93117
}
94118

95-
fn new_from_root(root_path: &str) -> DiskResolver {
96-
let root = PathBuf::from(root_path);
119+
fn new_from_root<P: AsRef<Path>>(root_path: P) -> DiskResolver {
120+
let root = root_path.as_ref().to_path_buf();
97121
let base_paths = vec![
98122
root.join("p"),
99123
root.join("p").join("48"),
@@ -103,6 +127,10 @@ impl DiskResolver {
103127
DiskResolver { base_paths }
104128
}
105129

130+
fn add_path<P: AsRef<Path>>(&mut self, path: P) {
131+
self.base_paths.push(path.as_ref().to_path_buf());
132+
}
133+
106134
/// Resolve a relative LDraw filename reference into an actual path on disk.
107135
fn resolve_path(&self, filename: &str) -> Result<PathBuf, ResolveError> {
108136
for prefix in &self.base_paths {
@@ -432,8 +460,29 @@ fn convert(app: &mut App, args: &ConvertCommand) -> Result<(), Error> {
432460
}
433461
let input = input.unwrap();
434462

463+
let mut resolver = DiskResolver::new();
464+
if let Some(catalog_path) = &args.catalog_path {
465+
resolver = DiskResolver::new_from_root(catalog_path);
466+
} else if let Ok(cwd) = std::env::current_dir() {
467+
eprintln!(
468+
"{}: No catalog path specified; using current working directory: {}",
469+
Yellow.paint("warning"),
470+
cwd.to_str().unwrap_or("(invalid path)")
471+
);
472+
resolver = DiskResolver::new_from_root(cwd);
473+
} else {
474+
app.print_error_and_exit(
475+
"No include/catalog path specified, and cannot use current directory. \
476+
Use -C/--catalog-path to specify the location of the catalog.",
477+
)
478+
}
479+
if let Some(include_paths) = &args.include_paths {
480+
for path in include_paths {
481+
resolver.add_path(path);
482+
}
483+
}
484+
435485
eprintln!("Parsing file '{}'", input);
436-
let resolver = DiskResolver::new_from_root("F:\\dev\\weldr\\data");
437486
let mut source_map = weldr::SourceMap::new();
438487
let source_file_ref = weldr::parse(input, &resolver, &mut source_map)?;
439488

0 commit comments

Comments
 (0)