@@ -31,6 +31,7 @@ use std::time::Duration;
3131use amzn_codewhisperer_client:: types:: SubscriptionStatus ;
3232use clap:: {
3333 Args ,
34+ CommandFactory ,
3435 Parser ,
3536} ;
3637use context:: ContextManager ;
@@ -1240,7 +1241,13 @@ impl ChatSession {
12401241 }
12411242 }
12421243 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+
12441251 match SlashCommand :: try_parse_from ( args) {
12451252 Ok ( command) => {
12461253 match command. execute ( os, self ) . await {
@@ -1259,7 +1266,35 @@ impl ChatSession {
12591266 writeln ! ( self . stderr) ?;
12601267 } ,
12611268 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+ }
12631298 } ,
12641299 }
12651300
0 commit comments