use std::fmt;
use clap::{CommandFactory, Parser, ValueEnum};
#[derive(Debug)]
pub struct UsageError(pub String);
impl fmt::Display for UsageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for UsageError {}
#[derive(Clone, Copy, Debug, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
Geojson,
Wkt,
}
#[derive(Debug, PartialEq)]
pub struct CliOptions {
pub input: Option<String>,
pub format: OutputFormat,
pub output: Option<String>,
pub centroid_first: bool,
pub quiet: bool,
pub verify: bool,
pub help: bool,
}
#[derive(Debug, Parser)]
#[command(
name = "interior-point",
about = "Compute an interior point of each input geometry.",
disable_help_flag = true,
disable_version_flag = true
)]
struct Cli {
#[arg(short, long, value_name = "geom|file", overrides_with = "input")]
input: Option<String>,
#[arg(short, long, value_name = "fmt", value_enum, default_value_t = OutputFormat::Geojson, overrides_with = "format")]
format: OutputFormat,
#[arg(short, long, value_name = "file", overrides_with = "output")]
output: Option<String>,
#[arg(short, long, overrides_with = "centroid_first")]
centroid_first: bool,
#[arg(short, long, overrides_with = "quiet")]
quiet: bool,
#[arg(short, long, overrides_with = "verify")]
verify: bool,
#[arg(short, long, overrides_with = "help")]
help: bool,
}
pub fn help_text() -> String {
Cli::command().render_help().to_string()
}
pub fn parse_cli_args(argv: &[String]) -> Result<CliOptions, UsageError> {
let with_name = std::iter::once("interior-point").chain(argv.iter().map(String::as_str));
let cli = Cli::try_parse_from(with_name).map_err(|e| UsageError(e.to_string()))?;
Ok(CliOptions {
input: cli.input,
format: cli.format,
output: cli.output,
centroid_first: cli.centroid_first,
quiet: cli.quiet,
verify: cli.verify,
help: cli.help,
})
}