Moving clap to version 4 (#1468)

* Moving clap to vesion 4

* Moving the sea-orm-migration cli to clap v4

* Removing the deprecated flag from clapv4

* Upgrade clap to 4.3

* Fixup

* .

* Try compile [CLI]

---------

Co-authored-by: aadi58002 <aadi58002gmail.com>
Co-authored-by: Billy Chan <ccw.billy.123@gmail.com>
This commit is contained in:
Aditya Yadav 2023-06-03 09:11:59 +05:30 committed by GitHub
parent 00c90b2d79
commit f6c9d81918
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 112 deletions

View File

@ -34,7 +34,7 @@ path = "src/bin/sea.rs"
required-features = ["cli", "codegen"] required-features = ["cli", "codegen"]
[dependencies] [dependencies]
clap = { version = "3.2", default-features = false, features = ["std", "env", "derive"], optional = true } clap = { version = "4.3", features = ["env", "derive"], optional = true }
dotenvy = { version = "0.15", default-features = false, optional = true } dotenvy = { version = "0.15", default-features = false, optional = true }
async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true } async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true }
sea-orm-codegen = { version = "=0.12.0", path = "../sea-orm-codegen", default-features = false, optional = true } sea-orm-codegen = { version = "=0.12.0", path = "../sea-orm-codegen", default-features = false, optional = true }

View File

@ -1,4 +1,4 @@
use clap::StructOpt; use clap::Parser;
use dotenvy::dotenv; use dotenvy::dotenv;
use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands};

View File

@ -1,6 +1,6 @@
//! COPY FROM bin/main.rs //! COPY FROM bin/main.rs
use clap::StructOpt; use clap::Parser;
use dotenvy::dotenv; use dotenvy::dotenv;
use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands};

View File

@ -1,7 +1,7 @@
use clap::{ArgEnum, ArgGroup, Parser, Subcommand}; use clap::{ArgGroup, Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[clap( #[command(
version, version,
author, author,
help_template = r#"{before-help}{name} {version} help_template = r#"{before-help}{name} {version}
@ -37,28 +37,27 @@ AUTHORS:
"# "#
)] )]
pub struct Cli { pub struct Cli {
#[clap(action, global = true, short, long, help = "Show debug messages")] #[arg(global = true, short, long, help = "Show debug messages")]
pub verbose: bool, pub verbose: bool,
#[clap(subcommand)] #[command(subcommand)]
pub command: Commands, pub command: Commands,
} }
#[derive(Subcommand, PartialEq, Eq, Debug)] #[derive(Subcommand, PartialEq, Eq, Debug)]
pub enum Commands { pub enum Commands {
#[clap( #[command(
about = "Codegen related commands", about = "Codegen related commands",
arg_required_else_help = true, arg_required_else_help = true,
display_order = 10 display_order = 10
)] )]
Generate { Generate {
#[clap(subcommand)] #[command(subcommand)]
command: GenerateSubcommands, command: GenerateSubcommands,
}, },
#[clap(about = "Migration related commands", display_order = 20)] #[command(about = "Migration related commands", display_order = 20)]
Migrate { Migrate {
#[clap( #[arg(
value_parser,
global = true, global = true,
short = 'd', short = 'd',
long, long,
@ -71,8 +70,7 @@ you should provide the directory of that submodule.",
)] )]
migration_dir: String, migration_dir: String,
#[clap( #[arg(
value_parser,
global = true, global = true,
short = 's', short = 's',
long, long,
@ -83,8 +81,7 @@ you should provide the directory of that submodule.",
)] )]
database_schema: Option<String>, database_schema: Option<String>,
#[clap( #[arg(
value_parser,
global = true, global = true,
short = 'u', short = 'u',
long, long,
@ -93,76 +90,59 @@ you should provide the directory of that submodule.",
)] )]
database_url: Option<String>, database_url: Option<String>,
#[clap(subcommand)] #[command(subcommand)]
command: Option<MigrateSubcommands>, command: Option<MigrateSubcommands>,
}, },
} }
#[derive(Subcommand, PartialEq, Eq, Debug)] #[derive(Subcommand, PartialEq, Eq, Debug)]
pub enum MigrateSubcommands { pub enum MigrateSubcommands {
#[clap(about = "Initialize migration directory", display_order = 10)] #[command(about = "Initialize migration directory", display_order = 10)]
Init, Init,
#[clap(about = "Generate a new, empty migration", display_order = 20)] #[command(about = "Generate a new, empty migration", display_order = 20)]
Generate { Generate {
#[clap( #[arg(required = true, help = "Name of the new migration")]
value_parser,
required = true,
takes_value = true,
help = "Name of the new migration"
)]
migration_name: String, migration_name: String,
#[clap( #[arg(
action,
long, long,
default_value = "true", default_value = "true",
help = "Generate migration file based on Utc time", help = "Generate migration file based on Utc time",
conflicts_with = "local-time", conflicts_with = "local_time",
display_order = 1001 display_order = 1001
)] )]
universal_time: bool, universal_time: bool,
#[clap( #[arg(
action,
long, long,
help = "Generate migration file based on Local time", help = "Generate migration file based on Local time",
conflicts_with = "universal-time", conflicts_with = "universal_time",
display_order = 1002 display_order = 1002
)] )]
local_time: bool, local_time: bool,
}, },
#[clap( #[command(
about = "Drop all tables from the database, then reapply all migrations", about = "Drop all tables from the database, then reapply all migrations",
display_order = 30 display_order = 30
)] )]
Fresh, Fresh,
#[clap( #[command(
about = "Rollback all applied migrations, then reapply all migrations", about = "Rollback all applied migrations, then reapply all migrations",
display_order = 40 display_order = 40
)] )]
Refresh, Refresh,
#[clap(about = "Rollback all applied migrations", display_order = 50)] #[command(about = "Rollback all applied migrations", display_order = 50)]
Reset, Reset,
#[clap(about = "Check the status of all migrations", display_order = 60)] #[command(about = "Check the status of all migrations", display_order = 60)]
Status, Status,
#[clap(about = "Apply pending migrations", display_order = 70)] #[command(about = "Apply pending migrations", display_order = 70)]
Up { Up {
#[clap( #[arg(short, long, help = "Number of pending migrations to apply")]
value_parser,
short,
long,
help = "Number of pending migrations to apply"
)]
num: Option<u32>, num: Option<u32>,
}, },
#[clap( #[command(about = "Rollback applied migrations", display_order = 80)]
value_parser,
about = "Rollback applied migrations",
display_order = 80
)]
Down { Down {
#[clap( #[arg(
value_parser,
short, short,
long, long,
default_value = "1", default_value = "1",
@ -175,54 +155,46 @@ pub enum MigrateSubcommands {
#[derive(Subcommand, PartialEq, Eq, Debug)] #[derive(Subcommand, PartialEq, Eq, Debug)]
pub enum GenerateSubcommands { pub enum GenerateSubcommands {
#[clap(about = "Generate entity")] #[command(about = "Generate entity")]
#[clap(arg_required_else_help = true)] #[command(group(ArgGroup::new("formats").args(&["compact_format", "expanded_format"])))]
#[clap(group(ArgGroup::new("formats").args(&["compact-format", "expanded-format"])))] #[command(group(ArgGroup::new("group-tables").args(&["tables", "include_hidden_tables"])))]
#[clap(group(ArgGroup::new("group-tables").args(&["tables", "include-hidden-tables"])))]
Entity { Entity {
#[clap(action, long, help = "Generate entity file of compact format")] #[arg(long, help = "Generate entity file of compact format")]
compact_format: bool, compact_format: bool,
#[clap(action, long, help = "Generate entity file of expanded format")] #[arg(long, help = "Generate entity file of expanded format")]
expanded_format: bool, expanded_format: bool,
#[clap( #[arg(
action,
long, long,
help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)" help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)"
)] )]
include_hidden_tables: bool, include_hidden_tables: bool,
#[clap( #[arg(
value_parser,
short = 't', short = 't',
long, long,
use_value_delimiter = true, value_delimiter = ',',
takes_value = true,
help = "Generate entity file for specified tables only (comma separated)" help = "Generate entity file for specified tables only (comma separated)"
)] )]
tables: Vec<String>, tables: Vec<String>,
#[clap( #[arg(
value_parser,
long, long,
use_value_delimiter = true, value_delimiter = ',',
takes_value = true,
default_value = "seaql_migrations", default_value = "seaql_migrations",
help = "Skip generating entity file for specified tables (comma separated)" help = "Skip generating entity file for specified tables (comma separated)"
)] )]
ignore_tables: Vec<String>, ignore_tables: Vec<String>,
#[clap( #[arg(
value_parser,
long, long,
default_value = "1", default_value = "1",
help = "The maximum amount of connections to use when connecting to the database." help = "The maximum amount of connections to use when connecting to the database."
)] )]
max_connections: u32, max_connections: u32,
#[clap( #[arg(
value_parser,
short = 'o', short = 'o',
long, long,
default_value = "./", default_value = "./",
@ -230,8 +202,7 @@ pub enum GenerateSubcommands {
)] )]
output_dir: String, output_dir: String,
#[clap( #[arg(
value_parser,
short = 's', short = 's',
long, long,
env = "DATABASE_SCHEMA", env = "DATABASE_SCHEMA",
@ -242,17 +213,10 @@ pub enum GenerateSubcommands {
)] )]
database_schema: String, database_schema: String,
#[clap( #[arg(short = 'u', long, env = "DATABASE_URL", help = "Database URL")]
value_parser,
short = 'u',
long,
env = "DATABASE_URL",
help = "Database URL"
)]
database_url: String, database_url: String,
#[clap( #[arg(
value_parser,
long, long,
default_value = "none", default_value = "none",
help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, \ help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, \
@ -260,23 +224,20 @@ pub enum GenerateSubcommands {
)] )]
with_serde: String, with_serde: String,
#[clap( #[arg(
action,
long, long,
help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'" help = "Generate a serde field attribute, '#[serde(skip_deserializing)]', for the primary key fields to skip them during deserialization, this flag will be affective only when '--with-serde' is 'both' or 'deserialize'"
)] )]
serde_skip_deserializing_primary_key: bool, serde_skip_deserializing_primary_key: bool,
#[clap( #[arg(
action,
long, long,
default_value = "false", default_value = "false",
help = "Opt-in to add skip attributes to hidden columns (i.e. when 'with-serde' enabled and column name starts with an underscore)" help = "Opt-in to add skip attributes to hidden columns (i.e. when 'with-serde' enabled and column name starts with an underscore)"
)] )]
serde_skip_hidden_column: bool, serde_skip_hidden_column: bool,
#[clap( #[arg(
action,
long, long,
default_value = "false", default_value = "false",
long_help = "Automatically derive the Copy trait on generated enums.\n\ long_help = "Automatically derive the Copy trait on generated enums.\n\
@ -286,17 +247,15 @@ pub enum GenerateSubcommands {
)] )]
with_copy_enums: bool, with_copy_enums: bool,
#[clap( #[arg(
arg_enum,
value_parser,
long, long,
default_value = "chrono", default_value_t,
value_enum,
help = "The datetime crate to use for generating entities." help = "The datetime crate to use for generating entities."
)] )]
date_time_crate: DateTimeCrate, date_time_crate: DateTimeCrate,
#[clap( #[arg(
action,
long, long,
short = 'l', short = 'l',
default_value = "false", default_value = "false",
@ -304,26 +263,21 @@ pub enum GenerateSubcommands {
)] )]
lib: bool, lib: bool,
#[clap( #[arg(
value_parser,
long, long,
use_value_delimiter = true, value_delimiter = ',',
takes_value = true,
help = "Add extra derive macros to generated model struct (comma separated), e.g. `--model-extra-derives 'ts_rs::Ts','CustomDerive'`" help = "Add extra derive macros to generated model struct (comma separated), e.g. `--model-extra-derives 'ts_rs::Ts','CustomDerive'`"
)] )]
model_extra_derives: Vec<String>, model_extra_derives: Vec<String>,
#[clap( #[arg(
value_parser,
long, long,
use_value_delimiter = true, value_delimiter = ',',
takes_value = true,
help = r#"Add extra attributes to generated model struct, no need for `#[]` (comma separated), e.g. `--model-extra-attributes 'serde(rename_all = "camelCase")','ts(export)'`"# help = r#"Add extra attributes to generated model struct, no need for `#[]` (comma separated), e.g. `--model-extra-attributes 'serde(rename_all = "camelCase")','ts(export)'`"#
)] )]
model_extra_attributes: Vec<String>, model_extra_attributes: Vec<String>,
#[clap( #[arg(
action,
long, long,
default_value = "false", default_value = "false",
long_help = "Generate helper Enumerations that are used by Seaography." long_help = "Generate helper Enumerations that are used by Seaography."
@ -332,8 +286,9 @@ pub enum GenerateSubcommands {
}, },
} }
#[derive(ArgEnum, Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum, Default)]
pub enum DateTimeCrate { pub enum DateTimeCrate {
#[default]
Chrono, Chrono,
Time, Time,
} }

View File

@ -237,7 +237,7 @@ impl From<DateTimeCrate> for CodegenDateTimeCrate {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use clap::StructOpt; use clap::Parser;
use super::*; use super::*;
use crate::{Cli, Commands}; use crate::{Cli, Commands};

View File

@ -21,7 +21,7 @@ path = "src/lib.rs"
[dependencies] [dependencies]
async-trait = { version = "0.1", default-features = false } async-trait = { version = "0.1", default-features = false }
clap = { version = "3.2", default-features = false, features = ["std", "env", "derive"], optional = true } clap = { version = "4.3", features = ["env", "derive"], optional = true }
dotenvy = { version = "0.15", default-features = false, optional = true } dotenvy = { version = "0.15", default-features = false, optional = true }
sea-orm = { version = "0.12.0", path = "../", default-features = false, features = ["macros"] } sea-orm = { version = "0.12.0", path = "../", default-features = false, features = ["macros"] }
sea-orm-cli = { version = "0.12.0", path = "../sea-orm-cli", default-features = false, optional = true } sea-orm-cli = { version = "0.12.0", path = "../sea-orm-cli", default-features = false, optional = true }

View File

@ -87,13 +87,12 @@ where
} }
#[derive(Parser)] #[derive(Parser)]
#[clap(version)] #[command(version)]
pub struct Cli { pub struct Cli {
#[clap(action, short = 'v', long, global = true, help = "Show debug messages")] #[arg(short = 'v', long, global = true, help = "Show debug messages")]
verbose: bool, verbose: bool,
#[clap( #[arg(
value_parser,
global = true, global = true,
short = 's', short = 's',
long, long,
@ -104,8 +103,7 @@ pub struct Cli {
)] )]
database_schema: Option<String>, database_schema: Option<String>,
#[clap( #[arg(
value_parser,
global = true, global = true,
short = 'u', short = 'u',
long, long,
@ -114,7 +112,7 @@ pub struct Cli {
)] )]
database_url: Option<String>, database_url: Option<String>,
#[clap(subcommand)] #[command(subcommand)]
command: Option<MigrateSubcommands>, command: Option<MigrateSubcommands>,
} }