6 use app
::parser
::Parser
;
8 pub struct PowerShellGen
<'a
, 'b
>
11 p
: &'b Parser
<'a
, 'b
>,
14 impl<'a
, 'b
> PowerShellGen
<'a
, 'b
> {
15 pub fn new(p
: &'b Parser
<'a
, 'b
>) -> Self { PowerShellGen { p: p }
}
17 pub fn generate_to
<W
: Write
>(&self, buf
: &mut W
) {
18 let bin_name
= self.p
.meta
.bin_name
.as_ref().unwrap();
20 let (subcommands_detection_cases
, subcommands_cases
) = generate_inner(self.p
, "");
22 let mut bin_names
= vec
![
24 format
!("./{0}", bin_name
),
27 bin_names
.push(format
!("{0}.exe", bin_name
));
28 bin_names
.push(format
!(r
".\{0}", bin_name
));
29 bin_names
.push(format
!(r
".\{0}.exe", bin_name
));
30 bin_names
.push(format
!(r
"./{0}.exe", bin_name
));
33 let bin_names
= bin_names
.iter().fold(String
::new(), |previous
, current
| {
34 format
!("{0}, '{1}'", previous
, current
)
36 let bin_names
= bin_names
.trim_left_matches(", ");
38 let result
= format
!(r
#"
40 Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {{
41 param($wordToComplete, $commandAst, $cursorPosition)
43 $command = '_{bin_name}'
44 $commandAst.CommandElements |
45 Select-Object -Skip 1 |
47 switch ($_.ToString()) {{
48 {subcommands_detection_cases}
59 ?{{ $_ -like "$wordToComplete*" }} |
61 %{{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }}
65 bin_names
= bin_names
,
67 subcommands_detection_cases
= subcommands_detection_cases
,
68 subcommands_cases
= subcommands_cases
71 w
!(buf
, result
.as_bytes());
75 fn generate_inner
<'a
, 'b
>(p
: &Parser
<'a
, 'b
>, previous_command_name
: &str) -> (String
, String
) {
76 debugln
!("PowerShellGen::generate_inner;");
77 let command_name
= format
!("{}_{}", previous_command_name
, &p
.meta
.name
);
79 let mut subcommands_detection_cases
= if previous_command_name
== "" {
91 let mut completions
= String
::new();
92 for subcommand
in &p
.subcommands
{
93 completions
.push_str(&format
!("'{}', ", &subcommand
.p
.meta
.name
));
95 for short
in &p
.short_list
{
96 completions
.push_str(&format
!("'-{}', ", short
));
98 for long
in &p
.long_list
{
99 completions
.push_str(&format
!("'--{}', ", long
));
102 let mut subcommands_cases
= format
!(r
"
108 completions
.trim_right_matches(", "));
110 for subcommand
in &p
.subcommands
{
111 let (subcommand_subcommands_detection_cases
, subcommand_subcommands_cases
) =
112 generate_inner(&subcommand
.p
, &command_name
);
113 subcommands_detection_cases
.push_str(&subcommand_subcommands_detection_cases
);
114 subcommands_cases
.push_str(&subcommand_subcommands_cases
);
117 (subcommands_detection_cases
, subcommands_cases
)