Skip to content

Commit d6aa351

Browse files
[Rust Server] Update bin/cli.rs to compile (#17876)
1 parent 7752287 commit d6aa351

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

modules/openapi-generator/src/main/resources/rust-server/Cargo.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ cli = [
7272
{{#apiHasDeleteMethods}}
7373
"dialoguer",
7474
{{/apiHasDeleteMethods}}
75-
"anyhow", "clap-verbosity-flag", "simple_logger", "structopt", "tokio"
75+
"anyhow", "clap", "clap-verbosity-flag", "simple_logger", "tokio"
7676
]
7777
conversion = ["frunk", "frunk_derives", "frunk_core", "frunk-enum-core", "frunk-enum-derive"]
7878

modules/openapi-generator/src/main/resources/rust-server/bin-cli.mustache

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! CLI tool driving the API client
22
use anyhow::{anyhow, Context, Result};
3+
use clap::Parser;
34
{{#apiHasDeleteMethods}}
45
use dialoguer::Confirm;
56
{{/apiHasDeleteMethods}}
@@ -19,7 +20,6 @@ use {{{externCrateName}}}::{
1920
{{/apiInfo}}
2021
};
2122
use simple_logger::SimpleLogger;
22-
use structopt::StructOpt;
2323
use swagger::{AuthData, ContextBuilder, EmptyContext, Push, XSpanIdString};
2424

2525
type ClientContext = swagger::make_context_ty!(
@@ -30,65 +30,65 @@ type ClientContext = swagger::make_context_ty!(
3030
);
3131

3232
{{! See code in RustServerCodegen if you are adding additional short option usage here. }}
33-
#[derive(StructOpt, Debug)]
34-
#[structopt(
33+
#[derive(Parser, Debug)]
34+
#[clap(
3535
name = "{{appName}}",
3636
version = "{{version}}",
3737
about = "CLI access to {{appName}}"
3838
)]
3939
struct Cli {
40-
#[structopt(subcommand)]
40+
#[clap(subcommand)]
4141
operation: Operation,
4242
4343
/// Address or hostname of the server hosting this API, including optional port
44-
#[structopt(short = "a", long, default_value = "http://localhost")]
44+
#[clap(short = 'a', long, default_value = "http://localhost")]
4545
server_address: String,
4646
4747
/// Path to the client private key if using client-side TLS authentication
4848
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
49-
#[structopt(long, requires_all(&["client-certificate", "server-certificate"]))]
49+
#[clap(long, requires_all(&["client-certificate", "server-certificate"]))]
5050
client_key: Option<String>,
5151
5252
/// Path to the client's public certificate associated with the private key
5353
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
54-
#[structopt(long, requires_all(&["client-key", "server-certificate"]))]
54+
#[clap(long, requires_all(&["client-key", "server-certificate"]))]
5555
client_certificate: Option<String>,
5656
5757
/// Path to CA certificate used to authenticate the server
5858
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
59-
#[structopt(long)]
59+
#[clap(long)]
6060
server_certificate: Option<String>,
6161
6262
/// If set, write output to file instead of stdout
63-
#[structopt(short, long)]
63+
#[clap(short, long)]
6464
output_file: Option<String>,
6565
66-
#[structopt(flatten)]
66+
#[command(flatten)]
6767
verbosity: clap_verbosity_flag::Verbosity,
6868
{{#apiHasDeleteMethods}}
6969
7070
/// Don't ask for any confirmation prompts
7171
#[allow(dead_code)]
72-
#[structopt(short, long)]
72+
#[clap(short, long)]
7373
force: bool,
7474
{{/apiHasDeleteMethods}}
7575
{{#hasHttpBearerMethods}}
7676

7777
/// Bearer token if used for authentication
78-
#[structopt(env = "{{#lambda.uppercase}}{{externCrateName}}{{/lambda.uppercase}}_BEARER_TOKEN", hide_env_values = true)]
78+
#[clap(env = "{{#lambda.uppercase}}{{externCrateName}}{{/lambda.uppercase}}_BEARER_TOKEN", hide_env_values = true)]
7979
bearer_token: Option<String>,
8080
{{/hasHttpBearerMethods}}
8181
{{^hasHttpBearerMethods}}
8282
{{#hasOAuthMethods}}
8383

8484
/// Bearer token if used for authentication
85-
#[structopt(env = "{{#lambda.uppercase}}{{externCrateName}}{{/lambda.uppercase}}_BEARER_TOKEN", hide_env_values = true)]
85+
#[clap(env = "{{#lambda.uppercase}}{{externCrateName}}{{/lambda.uppercase}}_BEARER_TOKEN", hide_env_values = true)]
8686
bearer_token: Option<String>,
8787
{{/hasOAuthMethods}}
8888
{{/hasHttpBearerMethods}}
8989
}
9090

91-
#[derive(StructOpt, Debug)]
91+
#[derive(Parser, Debug)]
9292
enum Operation {
9393
{{#apiInfo}}
9494
{{#apis}}
@@ -103,21 +103,21 @@ enum Operation {
103103
/// {{{description}}}
104104
{{/description}}
105105
{{^isPrimitiveType}}
106-
#[structopt(parse(try_from_str = parse_json){{#isArray}}, long{{/isArray}})]
106+
#[clap(value_parser = parse_json::<{{{dataType}}}>){{#isArray}}, long{{/isArray}}]
107107
{{/isPrimitiveType}}
108108
{{#isByteArray}}
109-
#[structopt(parse(try_from_str = parse_json))]
109+
#[clap(value_parser = parse_json::<{{{dataType}}}>)]
110110
{{/isByteArray}}
111111
{{#isBinary}}
112-
#[structopt(parse(try_from_str = parse_json))]
112+
#[clap(value_parser = parse_json::<{{{dataType}}}>)]
113113
{{/isBinary}}
114114
{{#isBoolean}}
115115
{{#isPrimitiveType}}
116116
{{#vendorExtensions.x-provide-cli-short-opt}}
117-
#[structopt(short, long)]
117+
#[clap(short, long)]
118118
{{/vendorExtensions.x-provide-cli-short-opt}}
119119
{{^vendorExtensions.x-provide-cli-short-opt}}
120-
#[structopt(long)]
120+
#[clap(long)]
121121
{{/vendorExtensions.x-provide-cli-short-opt}}
122122
{{/isPrimitiveType}}
123123
{{/isBoolean}}
@@ -165,7 +165,7 @@ fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoCont
165165

166166
#[tokio::main]
167167
async fn main() -> Result<()> {
168-
let args = Cli::from_args();
168+
let args = Cli::parse();
169169
if let Some(log_level) = args.verbosity.log_level() {
170170
SimpleLogger::new().with_level(log_level.to_level_filter()).init()?;
171171
}
@@ -312,6 +312,6 @@ fn prompt(force: bool, text: &str) -> Result<()> {
312312

313313
// May be unused if all inputs are primitive types
314314
#[allow(dead_code)]
315-
fn parse_json<'a, T: serde::de::Deserialize<'a>>(json_string: &'a str) -> Result<T> {
315+
fn parse_json<T: serde::de::DeserializeOwned>(json_string: &str) -> Result<T> {
316316
serde_json::from_str(json_string).map_err(|err| anyhow!("Error parsing input: {}", err))
317317
}

0 commit comments

Comments
 (0)