From f6c9d81918af7cc202ccd1db677d2ad29e3b50ff Mon Sep 17 00:00:00 2001 From: Aditya Yadav <96418027+aadi58002@users.noreply.github.com> Date: Sat, 3 Jun 2023 09:11:59 +0530 Subject: [PATCH] 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 Co-authored-by: Billy Chan --- sea-orm-cli/Cargo.toml | 2 +- sea-orm-cli/src/bin/main.rs | 2 +- sea-orm-cli/src/bin/sea.rs | 2 +- sea-orm-cli/src/cli.rs | 155 ++++++++++----------------- sea-orm-cli/src/commands/generate.rs | 2 +- sea-orm-migration/Cargo.toml | 2 +- sea-orm-migration/src/cli.rs | 12 +-- 7 files changed, 65 insertions(+), 112 deletions(-) diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index e40061ee..57dd5fee 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -34,7 +34,7 @@ path = "src/bin/sea.rs" required-features = ["cli", "codegen"] [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 } 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 } diff --git a/sea-orm-cli/src/bin/main.rs b/sea-orm-cli/src/bin/main.rs index e847d003..3a4e28ac 100644 --- a/sea-orm-cli/src/bin/main.rs +++ b/sea-orm-cli/src/bin/main.rs @@ -1,4 +1,4 @@ -use clap::StructOpt; +use clap::Parser; use dotenvy::dotenv; use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; diff --git a/sea-orm-cli/src/bin/sea.rs b/sea-orm-cli/src/bin/sea.rs index edf15c83..cef65391 100644 --- a/sea-orm-cli/src/bin/sea.rs +++ b/sea-orm-cli/src/bin/sea.rs @@ -1,6 +1,6 @@ //! COPY FROM bin/main.rs -use clap::StructOpt; +use clap::Parser; use dotenvy::dotenv; use sea_orm_cli::{handle_error, run_generate_command, run_migrate_command, Cli, Commands}; diff --git a/sea-orm-cli/src/cli.rs b/sea-orm-cli/src/cli.rs index d37c5ca0..1421646f 100644 --- a/sea-orm-cli/src/cli.rs +++ b/sea-orm-cli/src/cli.rs @@ -1,7 +1,7 @@ -use clap::{ArgEnum, ArgGroup, Parser, Subcommand}; +use clap::{ArgGroup, Parser, Subcommand, ValueEnum}; #[derive(Parser, Debug)] -#[clap( +#[command( version, author, help_template = r#"{before-help}{name} {version} @@ -37,28 +37,27 @@ AUTHORS: "# )] 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, - #[clap(subcommand)] + #[command(subcommand)] pub command: Commands, } #[derive(Subcommand, PartialEq, Eq, Debug)] pub enum Commands { - #[clap( + #[command( about = "Codegen related commands", arg_required_else_help = true, display_order = 10 )] Generate { - #[clap(subcommand)] + #[command(subcommand)] command: GenerateSubcommands, }, - #[clap(about = "Migration related commands", display_order = 20)] + #[command(about = "Migration related commands", display_order = 20)] Migrate { - #[clap( - value_parser, + #[arg( global = true, short = 'd', long, @@ -71,8 +70,7 @@ you should provide the directory of that submodule.", )] migration_dir: String, - #[clap( - value_parser, + #[arg( global = true, short = 's', long, @@ -83,8 +81,7 @@ you should provide the directory of that submodule.", )] database_schema: Option, - #[clap( - value_parser, + #[arg( global = true, short = 'u', long, @@ -93,76 +90,59 @@ you should provide the directory of that submodule.", )] database_url: Option, - #[clap(subcommand)] + #[command(subcommand)] command: Option, }, } #[derive(Subcommand, PartialEq, Eq, Debug)] pub enum MigrateSubcommands { - #[clap(about = "Initialize migration directory", display_order = 10)] + #[command(about = "Initialize migration directory", display_order = 10)] Init, - #[clap(about = "Generate a new, empty migration", display_order = 20)] + #[command(about = "Generate a new, empty migration", display_order = 20)] Generate { - #[clap( - value_parser, - required = true, - takes_value = true, - help = "Name of the new migration" - )] + #[arg(required = true, help = "Name of the new migration")] migration_name: String, - #[clap( - action, + #[arg( long, default_value = "true", help = "Generate migration file based on Utc time", - conflicts_with = "local-time", + conflicts_with = "local_time", display_order = 1001 )] universal_time: bool, - #[clap( - action, + #[arg( long, help = "Generate migration file based on Local time", - conflicts_with = "universal-time", + conflicts_with = "universal_time", display_order = 1002 )] local_time: bool, }, - #[clap( + #[command( about = "Drop all tables from the database, then reapply all migrations", display_order = 30 )] Fresh, - #[clap( + #[command( about = "Rollback all applied migrations, then reapply all migrations", display_order = 40 )] Refresh, - #[clap(about = "Rollback all applied migrations", display_order = 50)] + #[command(about = "Rollback all applied migrations", display_order = 50)] Reset, - #[clap(about = "Check the status of all migrations", display_order = 60)] + #[command(about = "Check the status of all migrations", display_order = 60)] Status, - #[clap(about = "Apply pending migrations", display_order = 70)] + #[command(about = "Apply pending migrations", display_order = 70)] Up { - #[clap( - value_parser, - short, - long, - help = "Number of pending migrations to apply" - )] + #[arg(short, long, help = "Number of pending migrations to apply")] num: Option, }, - #[clap( - value_parser, - about = "Rollback applied migrations", - display_order = 80 - )] + #[command(about = "Rollback applied migrations", display_order = 80)] Down { - #[clap( - value_parser, + #[arg( short, long, default_value = "1", @@ -175,54 +155,46 @@ pub enum MigrateSubcommands { #[derive(Subcommand, PartialEq, Eq, Debug)] pub enum GenerateSubcommands { - #[clap(about = "Generate entity")] - #[clap(arg_required_else_help = true)] - #[clap(group(ArgGroup::new("formats").args(&["compact-format", "expanded-format"])))] - #[clap(group(ArgGroup::new("group-tables").args(&["tables", "include-hidden-tables"])))] + #[command(about = "Generate entity")] + #[command(group(ArgGroup::new("formats").args(&["compact_format", "expanded_format"])))] + #[command(group(ArgGroup::new("group-tables").args(&["tables", "include_hidden_tables"])))] Entity { - #[clap(action, long, help = "Generate entity file of compact format")] + #[arg(long, help = "Generate entity file of compact format")] 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, - #[clap( - action, + #[arg( long, help = "Generate entity file for hidden tables (i.e. table name starts with an underscore)" )] include_hidden_tables: bool, - #[clap( - value_parser, + #[arg( short = 't', long, - use_value_delimiter = true, - takes_value = true, + value_delimiter = ',', help = "Generate entity file for specified tables only (comma separated)" )] tables: Vec, - #[clap( - value_parser, + #[arg( long, - use_value_delimiter = true, - takes_value = true, + value_delimiter = ',', default_value = "seaql_migrations", help = "Skip generating entity file for specified tables (comma separated)" )] ignore_tables: Vec, - #[clap( - value_parser, + #[arg( long, default_value = "1", help = "The maximum amount of connections to use when connecting to the database." )] max_connections: u32, - #[clap( - value_parser, + #[arg( short = 'o', long, default_value = "./", @@ -230,8 +202,7 @@ pub enum GenerateSubcommands { )] output_dir: String, - #[clap( - value_parser, + #[arg( short = 's', long, env = "DATABASE_SCHEMA", @@ -242,17 +213,10 @@ pub enum GenerateSubcommands { )] database_schema: String, - #[clap( - value_parser, - short = 'u', - long, - env = "DATABASE_URL", - help = "Database URL" - )] + #[arg(short = 'u', long, env = "DATABASE_URL", help = "Database URL")] database_url: String, - #[clap( - value_parser, + #[arg( long, default_value = "none", help = "Automatically derive serde Serialize / Deserialize traits for the entity (none, \ @@ -260,23 +224,20 @@ pub enum GenerateSubcommands { )] with_serde: String, - #[clap( - action, + #[arg( 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'" )] serde_skip_deserializing_primary_key: bool, - #[clap( - action, + #[arg( long, 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)" )] serde_skip_hidden_column: bool, - #[clap( - action, + #[arg( long, default_value = "false", long_help = "Automatically derive the Copy trait on generated enums.\n\ @@ -286,17 +247,15 @@ pub enum GenerateSubcommands { )] with_copy_enums: bool, - #[clap( - arg_enum, - value_parser, + #[arg( long, - default_value = "chrono", + default_value_t, + value_enum, help = "The datetime crate to use for generating entities." )] date_time_crate: DateTimeCrate, - #[clap( - action, + #[arg( long, short = 'l', default_value = "false", @@ -304,26 +263,21 @@ pub enum GenerateSubcommands { )] lib: bool, - #[clap( - value_parser, + #[arg( long, - use_value_delimiter = true, - takes_value = true, + value_delimiter = ',', help = "Add extra derive macros to generated model struct (comma separated), e.g. `--model-extra-derives 'ts_rs::Ts','CustomDerive'`" )] model_extra_derives: Vec, - #[clap( - value_parser, + #[arg( long, - use_value_delimiter = true, - takes_value = true, + value_delimiter = ',', 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, - #[clap( - action, + #[arg( long, default_value = "false", 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 { + #[default] Chrono, Time, } diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index af6d9f7b..7de19d9a 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -237,7 +237,7 @@ impl From for CodegenDateTimeCrate { #[cfg(test)] mod tests { - use clap::StructOpt; + use clap::Parser; use super::*; use crate::{Cli, Commands}; diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index a77eb316..73ab2008 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -21,7 +21,7 @@ path = "src/lib.rs" [dependencies] 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 } 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 } diff --git a/sea-orm-migration/src/cli.rs b/sea-orm-migration/src/cli.rs index a7a72539..aa0d3668 100644 --- a/sea-orm-migration/src/cli.rs +++ b/sea-orm-migration/src/cli.rs @@ -87,13 +87,12 @@ where } #[derive(Parser)] -#[clap(version)] +#[command(version)] 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, - #[clap( - value_parser, + #[arg( global = true, short = 's', long, @@ -104,8 +103,7 @@ pub struct Cli { )] database_schema: Option, - #[clap( - value_parser, + #[arg( global = true, short = 'u', long, @@ -114,7 +112,7 @@ pub struct Cli { )] database_url: Option, - #[clap(subcommand)] + #[command(subcommand)] command: Option, }