11use std:: ffi:: OsString ;
2+ use std:: io;
23
34use clap:: {
45 builder:: NonEmptyStringValueParser , crate_version, Arg , ArgAction , ArgMatches , Command ,
56 ValueHint ,
67} ;
8+ use clap_complete:: { generate, Shell } ;
79
810pub fn get_cli_arguments < ' a , I , T > ( args : I ) -> ArgMatches
911where
@@ -14,23 +16,53 @@ where
1416 command. get_matches_from ( args)
1517}
1618
19+ pub fn print_completions ( shell : & str , dest : & mut dyn io:: Write ) -> io:: Result < ( ) > {
20+ let shell = match shell {
21+ "bash" => Shell :: Bash ,
22+ "fish" => Shell :: Fish ,
23+ "zsh" => Shell :: Zsh ,
24+ "powershell" => Shell :: PowerShell ,
25+ "elvish" => Shell :: Elvish ,
26+ other => {
27+ return Err ( io:: Error :: new (
28+ io:: ErrorKind :: InvalidInput ,
29+ format ! ( "unknown shell for completions: {other}" ) ,
30+ ) ) ;
31+ }
32+ } ;
33+ generate ( shell, & mut build_command ( ) , "hyperfine" , dest) ;
34+ Ok ( ( ) )
35+ }
36+
1737/// Build the clap command for parsing command line arguments
18- fn build_command ( ) -> Command {
38+ pub fn build_command ( ) -> Command {
1939 Command :: new ( "hyperfine" )
2040 . version ( crate_version ! ( ) )
2141 . next_line_help ( true )
2242 . hide_possible_values ( true )
2343 . about ( "A command-line benchmarking tool." )
2444 . help_expected ( true )
2545 . max_term_width ( 80 )
46+ . arg (
47+ Arg :: new ( "gen-completions" )
48+ . long ( "gen-completions" )
49+ . alias ( "generate-shell-completion" )
50+ . value_name ( "SHELL" )
51+ . action ( ArgAction :: Set )
52+ . help (
53+ "Generate shell completions for SHELL to stdout. \
54+ [possible values: bash, fish, zsh, powershell, elvish]",
55+ )
56+ . value_parser ( [ "bash" , "fish" , "zsh" , "powershell" , "elvish" ] ) ,
57+ )
2658 . arg (
2759 Arg :: new ( "command" )
2860 . help ( "The command to benchmark. This can be the name of an executable, a command \
2961 line like \" grep -i todo\" or a shell command like \" sleep 0.5 && echo test\" . \
3062 The latter is only available if the shell is not explicitly disabled via \
3163 '--shell=none'. If multiple commands are given, hyperfine will show a \
3264 comparison of the respective runtimes.")
33- . required ( true )
65+ . required_unless_present ( "gen-completions" )
3466 . action ( ArgAction :: Append )
3567 . value_hint ( ValueHint :: CommandString )
3668 . value_parser ( NonEmptyStringValueParser :: new ( ) ) ,
0 commit comments