47 lines
1.8 KiB
Rust
47 lines
1.8 KiB
Rust
//! Curl argument parsing and process exit codes. HTTP behavior lives in the library.
|
|
use clap::{CommandFactory, FromArgMatches};
|
|
fn main() {
|
|
let args = match curl::Args::command().try_get_matches() {
|
|
Ok(matches) => {
|
|
let mut args = curl::Args::from_arg_matches(&matches).unwrap();
|
|
let mut ordered = Vec::new();
|
|
for (name, kind) in [("data", 0), ("raw", 1), ("binary", 2), ("json", 3)] {
|
|
if let Some(indices) = matches.indices_of(name) {
|
|
for (index, value) in indices.zip(matches.get_many::<String>(name).unwrap()) {
|
|
ordered.push((index, value.clone(), kind));
|
|
}
|
|
}
|
|
}
|
|
ordered.sort_by_key(|p| p.0);
|
|
args.parts = ordered
|
|
.into_iter()
|
|
.map(|(_, value, kind)| (value, kind))
|
|
.collect();
|
|
args
|
|
}
|
|
Err(error) => {
|
|
if !error.use_stderr() {
|
|
print!("{error}");
|
|
return;
|
|
}
|
|
if error.kind() == clap::error::ErrorKind::UnknownArgument {
|
|
if let Some(option) = error.get(clap::error::ContextKind::InvalidArg) {
|
|
eprintln!("curl: option {option}: is unknown");
|
|
eprintln!("curl: try 'curl --help' for more information");
|
|
} else {
|
|
eprintln!("curl: {error}");
|
|
}
|
|
} else {
|
|
eprintln!("curl: {error}");
|
|
}
|
|
std::process::exit(2);
|
|
}
|
|
};
|
|
if let Err(error) = curl::run(&args) {
|
|
if !args.silent || args.show_error {
|
|
eprintln!("curl: ({}) {}", error.code, error.message);
|
|
}
|
|
std::process::exit(error.code);
|
|
}
|
|
}
|