Simplify cargo-sqlx cmdline definition (#1626)

* Simplify cargo-sqlx cmdline definition

* Add note about the parser definition for cargo-sqlx

* Fix formatting
This commit is contained in:
Marcin Puc
2022-02-16 05:13:42 +01:00
committed by GitHub
parent 2849468e57
commit 313cc69988
3 changed files with 26 additions and 21 deletions

View File

@@ -33,7 +33,7 @@ sqlx = { version = "0.5.10", path = "..", default-features = false, features = [
"offline",
] }
futures = "0.3.19"
clap = { version = "3.0.0-rc.9", features = ["derive", "env", "cargo"] }
clap = { version = "3.0", features = ["derive", "env"] }
chrono = "0.4.19"
anyhow = "1.0.52"
url = { version = "2.2.2", default-features = false }

View File

@@ -1,22 +1,21 @@
use clap::{crate_version, AppSettings, FromArgMatches, IntoApp};
use clap::Parser;
use console::style;
use dotenv::dotenv;
use sqlx_cli::Opt;
use std::{env, process};
use std::process;
// cargo invokes this binary as `cargo-sqlx sqlx <args>`
// so the parser below is defined with that in mind
#[derive(Parser, Debug)]
#[clap(bin_name = "cargo")]
enum Cli {
Sqlx(Opt),
}
#[tokio::main]
async fn main() {
// when invoked as `cargo sqlx [...]` the args we see are `[...]/sqlx-cli sqlx prepare`
// so we want to notch out that superfluous "sqlx"
let args = env::args_os().skip(2);
dotenv().ok();
let matches = Opt::into_app()
.bin_name("cargo sqlx")
.setting(AppSettings::NoBinaryName)
.get_matches_from(args);
let opt = Opt::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
let Cli::Sqlx(opt) = Cli::parse();
if let Err(error) = sqlx_cli::run(opt).await {
println!("{} {}", style("error:").bold().red(), error);