@@ -31,6 +31,7 @@ use std::time::Duration;
31
31
use amzn_codewhisperer_client:: types:: SubscriptionStatus ;
32
32
use clap:: {
33
33
Args ,
34
+ CommandFactory ,
34
35
Parser ,
35
36
} ;
36
37
use context:: ContextManager ;
@@ -1240,7 +1241,13 @@ impl ChatSession {
1240
1241
}
1241
1242
}
1242
1243
if let Some ( mut args) = input. strip_prefix ( "/" ) . and_then ( shlex:: split) {
1243
- args. insert ( 0 , "q" . to_owned ( ) ) ;
1244
+ // Knowing the first argument is required for error handling.
1245
+ let first_arg = args. first ( ) . cloned ( ) ;
1246
+
1247
+ // We set the binary name as a dummy name "slash_command" which we
1248
+ // replace anytime we error out and print a usage statement.
1249
+ args. insert ( 0 , "slash_command" . to_owned ( ) ) ;
1250
+
1244
1251
match SlashCommand :: try_parse_from ( args) {
1245
1252
Ok ( command) => {
1246
1253
match command. execute ( os, self ) . await {
@@ -1259,7 +1266,35 @@ impl ChatSession {
1259
1266
writeln ! ( self . stderr) ?;
1260
1267
} ,
1261
1268
Err ( err) => {
1262
- writeln ! ( self . stderr, "{}" , err. render( ) . ansi( ) ) ?;
1269
+ // Replace the dummy name with a slash. Also have to check for an ansi sequence
1270
+ // for invalid slash commands (e.g. on a "/doesntexist" input).
1271
+ let ansi_output = err
1272
+ . render ( )
1273
+ . ansi ( )
1274
+ . to_string ( )
1275
+ . replace ( "slash_command " , "/" )
1276
+ . replace ( "slash_command\u{1b} [0m " , "/" ) ;
1277
+
1278
+ writeln ! ( self . stderr, "{}" , ansi_output) ?;
1279
+
1280
+ // Print the subcommand help, if available. Required since by default we won't
1281
+ // show what the actual arguments are, requiring an unnecessary --help call.
1282
+ if let (
1283
+ clap:: error:: ErrorKind :: InvalidValue
1284
+ | clap:: error:: ErrorKind :: UnknownArgument
1285
+ | clap:: error:: ErrorKind :: InvalidSubcommand
1286
+ | clap:: error:: ErrorKind :: MissingRequiredArgument ,
1287
+ Some ( first_arg) ,
1288
+ ) = ( err. kind ( ) , first_arg)
1289
+ {
1290
+ let mut cmd = SlashCommand :: command ( ) ;
1291
+ let help = if let Some ( subcmd) = cmd. find_subcommand_mut ( first_arg) {
1292
+ subcmd. to_owned ( ) . help_template ( "{all-args}" ) . render_help ( )
1293
+ } else {
1294
+ cmd. help_template ( "{all-args}" ) . render_help ( )
1295
+ } ;
1296
+ writeln ! ( self . stderr, "{}" , help. ansi( ) ) ?;
1297
+ }
1263
1298
} ,
1264
1299
}
1265
1300
0 commit comments