1
1
//! CLI tool driving the API client
2
2
use anyhow::{ anyhow, Context, Result} ;
3
+ use clap::Parser;
3
4
{ {#apiHasDeleteMethods} }
4
5
use dialoguer::Confirm;
5
6
{ {/apiHasDeleteMethods} }
@@ -19,7 +20,6 @@ use {{{externCrateName}}}::{
19
20
{ {/apiInfo} }
20
21
};
21
22
use simple_logger::SimpleLogger;
22
- use structopt::StructOpt;
23
23
use swagger::{ AuthData, ContextBuilder, EmptyContext, Push, XSpanIdString} ;
24
24
25
25
type ClientContext = swagger::make_context_ty!(
@@ -30,65 +30,65 @@ type ClientContext = swagger::make_context_ty!(
30
30
);
31
31
32
32
{ {! 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 (
35
35
name = "{ {appName} }",
36
36
version = "{ {version} }",
37
37
about = "CLI access to { {appName} }"
38
38
)]
39
39
struct Cli {
40
- #[structopt (subcommand)]
40
+ #[clap (subcommand)]
41
41
operation: Operation,
42
42
43
43
/// 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" )]
45
45
server_address: String,
46
46
47
47
/// Path to the client private key if using client-side TLS authentication
48
48
#[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" ]))]
50
50
client_key: Option< String> ,
51
51
52
52
/// Path to the client' s public certificate associated with the private key
53
53
#[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"]))]
55
55
client_certificate: Option<String>,
56
56
57
57
/// Path to CA certificate used to authenticate the server
58
58
#[cfg(not(any(target_os = "macos", target_os = "windows", target_os = "ios")))]
59
- #[structopt (long)]
59
+ #[clap (long)]
60
60
server_certificate: Option<String>,
61
61
62
62
/// If set, write output to file instead of stdout
63
- #[structopt (short, long)]
63
+ #[clap (short, long)]
64
64
output_file: Option<String>,
65
65
66
- #[structopt (flatten)]
66
+ #[command (flatten)]
67
67
verbosity: clap_verbosity_flag::Verbosity,
68
68
{{#apiHasDeleteMethods}}
69
69
70
70
/// Don' t ask for any confirmation prompts
71
71
#[allow(dead_code)]
72
- #[structopt (short, long)]
72
+ #[clap (short, long)]
73
73
force: bool,
74
74
{{/apiHasDeleteMethods} }
75
75
{ {#hasHttpBearerMethods} }
76
76
77
77
/// 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)]
79
79
bearer_token: Option<String >,
80
80
{ {/hasHttpBearerMethods} }
81
81
{ {^hasHttpBearerMethods} }
82
82
{ {#hasOAuthMethods} }
83
83
84
84
/// 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)]
86
86
bearer_token: Option<String >,
87
87
{ {/hasOAuthMethods} }
88
88
{ {/hasHttpBearerMethods} }
89
89
}
90
90
91
- #[derive(StructOpt , Debug)]
91
+ #[derive(Parser , Debug)]
92
92
enum Operation {
93
93
{{#apiInfo} }
94
94
{ {#apis} }
@@ -103,21 +103,21 @@ enum Operation {
103
103
/// { {{description} }}
104
104
{ {/description} }
105
105
{ {^isPrimitiveType} }
106
- #[structopt(parse(try_from_str = parse_json){ {#isArray} }, long{ {/isArray} }) ]
106
+ #[clap(value_parser = parse_json::< { {{dataType } }}> ){ {#isArray} }, long{ {/isArray} }]
107
107
{ {/isPrimitiveType} }
108
108
{ {#isByteArray} }
109
- #[structopt(parse(try_from_str = parse_json) )]
109
+ #[clap(value_parser = parse_json::< { {{dataType } }}> )]
110
110
{ {/isByteArray} }
111
111
{ {#isBinary} }
112
- #[structopt(parse(try_from_str = parse_json) )]
112
+ #[clap(value_parser = parse_json::< { {{dataType } }}> )]
113
113
{ {/isBinary} }
114
114
{ {#isBoolean} }
115
115
{ {#isPrimitiveType} }
116
116
{ {#vendorExtensions.x-provide-cli-short-opt} }
117
- #[structopt (short, long)]
117
+ #[clap (short, long)]
118
118
{ {/vendorExtensions.x-provide-cli-short-opt} }
119
119
{ {^vendorExtensions.x-provide-cli-short-opt} }
120
- #[structopt (long)]
120
+ #[clap (long)]
121
121
{ {/vendorExtensions.x-provide-cli-short-opt} }
122
122
{ {/isPrimitiveType} }
123
123
{ {/isBoolean} }
@@ -165,7 +165,7 @@ fn create_client(args: &Cli, context: ClientContext) -> Result<Box<dyn ApiNoCont
165
165
166
166
#[tokio::main]
167
167
async fn main() -> Result<()> {
168
- let args = Cli::from_args ();
168
+ let args = Cli::parse ();
169
169
if let Some(log_level) = args.verbosity.log_level() {
170
170
SimpleLogger::new().with_level(log_level.to_level_filter()).init()?;
171
171
}
@@ -312,6 +312,6 @@ fn prompt(force: bool, text: &str) -> Result<()> {
312
312
313
313
// May be unused if all inputs are primitive types
314
314
#[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 > {
316
316
serde_json::from_str(json_string).map_err(|err| anyhow! (" Error parsing input: {}" , err))
317
317
}
0 commit comments