Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ anyhow = "1.0"
clap = { version = "4.3", features = ["derive"] }
colored = "2.0"
loop_lib = { path = "../loop_lib" }
# Any 0.x, as for loop_lib
meta_core = { path = "../meta_core", version = "<1" }
rayon = "1.7"
serde_json = "1.0"
walkdir = "2.3"
Expand Down
41 changes: 37 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct Cli {
}

fn main() -> Result<()> {
let cli = Cli::parse();
let cli = parse_cli();

let config_path = cli.config.unwrap_or_else(|| PathBuf::from(".looprc"));

Expand All @@ -50,13 +50,19 @@ fn main() -> Result<()> {
std::process::exit(0);
}

// Parse the .meta file
// Parse the .looprc file
let absolute_path = std::env::current_dir()?.join(&config_path);

if cli.verbose {
println!("{}", "Verbose mode enabled".green());
println!("\nResolved config file path: {}", absolute_path.display());
println!("Executing command: {}", display_command(&cli.command));
println!(
"\nResolved config file path: {}",
meta_core::display::escape_path(&absolute_path)
);
println!(
"Executing command: {}",
meta_core::display::escape(&display_command(&cli.command))
);
}

let mut config = if config_path.exists() {
Expand Down Expand Up @@ -87,3 +93,30 @@ fn main() -> Result<()> {

Ok(())
}

/// Parse the command line. Clap quotes rejected values as given, so an error
/// is shown from the escaped arguments: the same error, with values that
/// can't move the cursor or forge a line.
fn parse_cli() -> Cli {
use clap::error::ErrorKind;
Cli::try_parse().unwrap_or_else(|e| match e.kind() {
// clap's invalid UTF-8 error doesn't echo the value
ErrorKind::DisplayHelp
| ErrorKind::DisplayVersion
| ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand
| ErrorKind::InvalidUtf8 => e.exit(),
_ => {
let shown = std::env::args_os()
.map(|arg| meta_core::display::escape(&arg.to_string_lossy()).into_owned());
match Cli::try_parse_from(shown) {
Err(shown) => shown.exit(),
Ok(_) => Cli::command()
.error(
ErrorKind::InvalidValue,
"an argument contains a control character",
)
.exit(),
}
}
})
}
Loading