Skip to content

Commit

Permalink
chore: move from structopt to clap v4 with derive
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Oct 19, 2023
1 parent 9e99a21 commit 04e2fb7
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 160 deletions.
26 changes: 21 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ homepage = "https://github.com/Joxit/wof-cli"
documentation = "https://docs.rs/wof/"
readme = "README.md"
keywords = ["cli"]
categories = ["command-line-utilities", "database-implementations", "data-structures", "parser-implementations"]
categories = [
"command-line-utilities",
"database-implementations",
"data-structures",
"parser-implementations",
]

[[bin]]
name = "wof"
Expand All @@ -29,10 +34,11 @@ postgres = "^0.19.3"
lazy_static = "^1.4.0"
which = { version = "^4.4.2", optional = true }
tar = { version = "^0.4.26", optional = true }
flate2 = { version ="^1.0.13", optional = true }
structopt = { version = "^0.3", optional = true }
flate2 = { version = "^1.0.13", optional = true }
attohttpc = { version = "^0.26.1", optional = true }
chrono = { version = "^0.4.10", optional = true }
clap = { version = "^4.4", features = ["derive", "env"], optional = true }
clap_complete = { version = "^4.4", optional = true }

[dependencies.gdal]
version = "^0.16"
Expand All @@ -54,5 +60,15 @@ version = "0.29.0"
features = ["bundled"]

[features]
cli = ["which", "tar", "flate2", "structopt", "attohttpc", "chrono", "log", "git2"]
with-gdal = ["gdal"]
cli = [
"which",
"tar",
"flate2",
"clap",
"clap_complete",
"attohttpc",
"chrono",
"log",
"git2",
]
with-gdal = ["gdal"]
10 changes: 5 additions & 5 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ pub use crate::commands::build::postgres::Postgres;
pub use crate::commands::build::shapefile::Shapefile;
pub use crate::commands::build::sqlite::SQLite;
use crate::repo::Walk;
use clap::Parser;
use log::{error, info};
use std::path::PathBuf;
use std::time::SystemTime;
use structopt::StructOpt;

mod postgres;
mod shapefile;
mod sqlite;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub enum Build {
/// Who's On First documents to PostgreSQL database.
#[structopt(name = "postgres")]
#[command(name = "postgres")]
Postgres(Postgres),
/// Who's On First documents to ESRI shapefiles.
#[structopt(name = "shapefile")]
#[command(name = "shapefile")]
Shapefile(Shapefile),
/// Who's On First documents to SQLite database.
#[structopt(name = "sqlite")]
#[command(name = "sqlite")]
SQLite(SQLite),
}

Expand Down
32 changes: 16 additions & 16 deletions src/commands/build/postgres.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
use crate::postgres;
use crate::utils::ResultExit;
use clap::Parser;
use log::info;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Postgres {
/// WOF data directories to import
#[structopt(default_value = ".")]
#[arg(default_value = ".")]
pub directories: Vec<String>,
/// The IP or hostname of the postgreSQL database.
#[structopt(long = "host", default_value = "127.0.0.1", env = "WOF_PG_HOST")]
#[arg(long = "host", default_value = "127.0.0.1", env = "WOF_PG_HOST")]
pub host: String,
/// The postgreSQL user name to use.
#[structopt(
short = "u",
#[arg(
short = 'u',
long = "user",
default_value = "wof",
env = "WOF_PG_USERNAME"
)]
pub user: String,
/// The postgreSQL database name to use.
#[structopt(
short = "d",
#[arg(
short = 'd',
long = "dbname",
default_value = "gis",
env = "WOF_PG_DBNAME"
)]
pub dbname: String,
/// The postgreSQL database port to use.
#[structopt(
short = "p",
#[arg(
short = 'p',
long = "port",
default_value = "5432",
env = "WOF_PG_DBNAME"
)]
pub port: u16,
/// The postgreSQL database port to use.
#[structopt(short = "W", long = "password", env = "WOF_PG_PASSWORD")]
#[arg(short = 'W', long = "password", env = "WOF_PG_PASSWORD")]
pub password: Option<String>,
/// The SIRID to use for geometry storage. Default value is 4326, common usage is also 3857.
#[structopt(
short = "s",
#[arg(
short = 's',
long = "srid",
default_value = "4326",
env = "WOF_PG_SRID"
)]
pub srid: i32,
/// Don't insert deprecated features.
#[structopt(long = "no-deprecated")]
#[arg(long = "no-deprecated")]
pub no_deprecated: bool,
/// Display timings during the build process, implies verbose.
#[structopt(long = "timings")]
#[arg(long = "timings")]
pub timings: bool,
/// Activate verbose mode.
#[structopt(short = "v", long = "verbose")]
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
}

Expand Down
29 changes: 15 additions & 14 deletions src/commands/build/shapefile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,46 @@ use crate::shapefile;
use crate::utils::ResultExit;
use crate::wof::WOFGeoJSON;
use crate::JsonValue;
use clap::builder::PossibleValuesParser;
use clap::Parser;
use log::{error, info};
use std::path::Path;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Shapefile {
#[structopt(default_value = ".")]
#[arg(default_value = ".")]
pub directories: Vec<String>,
/// Include only records that belong to this ID. You may pass multiple -belongs-to flags.
#[structopt(long = "belongs-to")]
#[arg(long = "belongs-to")]
pub belongs_to: Option<Vec<i32>>,
/// Exclude records of this placetype. You may pass multiple -exclude-placetype flags.
#[structopt(long = "exclude-placetype")]
#[arg(long = "exclude-placetype")]
pub exclude: Option<Vec<String>>,
/// Include only records of this placetype. You may pass multiple -include-placetype flags.
#[structopt(long = "include-placetype")]
#[arg(long = "include-placetype")]
pub include: Option<Vec<String>>,
/// The mode to use importing data.
#[structopt(
#[arg(
long = "mode",
possible_values = &["directory", "feature", "feature-collection", "files", "geojson-ls", "meta", "path", "repo", "sqlite"],
value_parser = PossibleValuesParser::new(&["directory", "feature", "feature-collection", "files", "geojson-ls", "meta", "path", "repo", "sqlite"]),
default_value = "repo")]
pub mode: String,
/// Where to write the new shapefile.
#[structopt(long = "out", default_value = "whosonfirst-data-latest.shp")]
#[arg(long = "out", default_value = "whosonfirst-data-latest.shp")]
pub out: String,
// todo: "MULTIPOINT"
/// The shapefile type to use indexing data.
#[structopt(
#[arg(
long = "shapetype",
possible_values = &["POINT", "POLYLINE", "POLYGON"],
case_insensitive = false,
value_parser = PossibleValuesParser::new(&["POINT", "POLYLINE", "POLYGON"]),
ignore_case = false,
default_value = "POLYGON")]
pub shapetype: String,
/// Activate verbose mode.
#[structopt(short = "v", long = "verbose")]
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
/// Display timings during and after indexing
#[structopt(long = "timings")]
#[arg(long = "timings")]
pub timings: bool,
}

Expand Down
19 changes: 10 additions & 9 deletions src/commands/build/sqlite.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
use crate::commands::assert_directory_exists;
use crate::sqlite;
use crate::utils::ResultExit;
use clap::builder::PossibleValuesParser;
use clap::Parser;
use log::info;
use std::path::Path;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct SQLite {
/// WOF data directories
#[structopt(default_value = ".")]
#[arg(default_value = ".")]
pub directories: Vec<String>,
/// Where to store the final build file. If empty the code will attempt to create whosonfirst-data-latest.db the current working directory.
#[structopt(long = "out", default_value = "whosonfirst-data-latest.db")]
#[arg(long = "out", default_value = "whosonfirst-data-latest.db")]
pub out: String,
/// Don't insert deprecated features.
#[structopt(long = "no-deprecated")]
#[arg(long = "no-deprecated")]
pub no_deprecated: bool,
/// Don't prettify the geojson.
#[structopt(long = "no-pretty")]
#[arg(long = "no-pretty")]
pub no_pretty: bool,
/// Preset for pelias use. Will insert only in geojson and spr tables.
#[structopt(long = "preset", possible_values = &["pelias"])]
#[arg(long = "preset", value_parser = PossibleValuesParser::new(&["pelias"]))]
pub preset: Option<String>,
/// Display timings during the build process, implies verbose.
#[structopt(long = "timings")]
#[arg(long = "timings")]
pub timings: bool,
/// Activate verbose mode.
#[structopt(short = "v", long = "verbose")]
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
}

Expand Down
20 changes: 11 additions & 9 deletions src/commands/completion.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::ApplicationArguments;
use structopt::clap::Shell;
use structopt::StructOpt;
use crate::Wof;
use clap::{CommandFactory, Parser};
use clap_complete::{generate, Shell};

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub enum Completion {
/// Generates a .bash completion file for the Bourne Again SHell (BASH)
/// Save the output in `/etc/bash_completion.d/wof` or `~/.local/share/bash-completion/completions/wof`
#[structopt(name = "bash")]
#[command(name = "bash")]
Bash,
/// Generates a .fish completion file for the Friendly Interactive SHell (fish)
#[structopt(name = "fish")]
#[command(name = "fish")]
Fish,
/// Generates a completion file for the Z SHell (ZSH)
#[structopt(name = "zsh")]
#[command(name = "zsh")]
Zsh,
/// Generates a completion file for Elvish
#[structopt(name = "elvish")]
#[command(name = "elvish")]
Elvish,
}

Expand All @@ -27,6 +27,8 @@ impl Completion {
Completion::Zsh => Shell::Zsh,
Completion::Elvish => Shell::Elvish,
};
ApplicationArguments::clap().gen_completions_to("wof", shell, &mut std::io::stdout());
let mut cli = Wof::command();
let bin_name = cli.get_name().to_string();
generate(shell, &mut cli, &bin_name, &mut std::io::stdout());
}
}
31 changes: 16 additions & 15 deletions src/commands/export.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
use crate::commands::Command;
use crate::git::Git;
use crate::utils::ResultExit;
use clap::builder::PossibleValuesParser;
use clap::Parser;
use std::default::Default;
use structopt::StructOpt;

static BINARY: &'static str = "wof-exportify";

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Export {
/// Where to write the export, on stdout or flatfile (needs source)
#[structopt(short = "e", long = "exporter", possible_values = &["flatfile", "stdout"])]
#[arg(short = 'e', long = "exporter", value_parser = PossibleValuesParser::new(&["flatfile", "stdout"]))]
pub exporter: Option<String>,
/// WOF data folder where are stored GeoJSONs to exportify
#[structopt(short = "s", long = "source")]
#[arg(short = 's', long = "source")]
pub source: Option<String>,
/// The WOF id of the object to export
#[structopt(short = "i", long = "id")]
#[arg(short = 'i', long = "id")]
pub id: Option<u32>,
/// Path of the object to export
#[structopt(short = "p", long = "path")]
#[arg(short = 'p', long = "path")]
pub path: Option<String>,
#[structopt(short = "c", long = "collection")]
#[arg(short = 'c', long = "collection")]
pub collection: bool,
#[structopt(short = "a", long = "alt")]
#[arg(short = 'a', long = "alt")]
pub alt: Option<String>,
#[structopt(short = "d", long = "display")]
#[arg(short = 'd', long = "display")]
pub display: Option<String>,
/// Read stdin for the object to export
#[structopt(long = "stdin")]
#[arg(long = "stdin")]
pub stdin: bool,
/// Run export on all files of a specific commit/ref (needs git repository)
#[structopt(long = "commit")]
#[arg(long = "commit")]
pub commit: Option<String>,
/// Run export on all stagged files (needs git repository)
#[structopt(long = "stagged")]
#[arg(long = "stagged")]
pub stagged: bool,
/// Activate debug mode
#[structopt(long = "debug")]
#[arg(long = "debug")]
pub debug: bool,
/// Activate verbose mode
#[structopt(short = "v", long = "verbose")]
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
#[structopt(skip)]
#[arg(skip)]
pub exit: bool,
}

Expand Down
15 changes: 8 additions & 7 deletions src/commands/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
use crate::commands::{download_tar_gz_stream_geojson, download_tar_gz_strip, output_pipe};
use crate::utils;
use crate::utils::ResultExit;
use clap::builder::PossibleValuesParser;
use clap::Parser;
use log::{error, info};
use std::path::Path;
use std::time::SystemTime;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[derive(Debug, Parser)]
pub struct Fetch {
/// Ouput directory to download WOF documents
#[structopt(short = "o", long = "out", default_value = ".")]
#[arg(short = 'o', long = "out", default_value = ".")]
pub out: String,
/// Should download postalcodes repositories
#[structopt(long = "postalcode", possible_values = &["true", "false"])]
#[arg(long = "postalcode", value_parser = PossibleValuesParser::new(&["true", "false"]))]
pub postalcode: Option<bool>,
/// Should download admin repositories, default true
#[structopt(long = "admin", possible_values = &["true", "false"])]
#[arg(long = "admin", value_parser =PossibleValuesParser::new( &["true", "false"]))]
pub admin: Option<bool>,
/// Two letters country code to download. No values will download all repositories.
pub countries: Vec<String>,
/// Display timings during the download process, implies verbose.
#[structopt(long = "timings")]
#[arg(long = "timings")]
pub timings: bool,
/// Activate verbose mode.
#[structopt(short = "v", long = "verbose")]
#[arg(short = 'v', long = "verbose")]
pub verbose: bool,
}

Expand Down
Loading

0 comments on commit 04e2fb7

Please sign in to comment.