Skip to content

Commit

Permalink
imp(PowerShell Completions): massively dedups subcommand names in the…
Browse files Browse the repository at this point in the history
… generate script to make smaller scripts that are still functionally equiv
  • Loading branch information
kbknapp committed Apr 19, 2017
1 parent a8bce55 commit 85b0e1c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/completions/powershell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl<'a, 'b> PowerShellGen<'a, 'b> {
pub fn generate_to<W: Write>(&self, buf: &mut W) {
let bin_name = self.p.meta.bin_name.as_ref().unwrap();

let (subcommands_detection_cases, subcommands_cases) = generate_inner(self.p, "");
let mut names = vec![];
let (subcommands_detection_cases, subcommands_cases) = generate_inner(self.p, "", &mut names);

let mut bin_names = vec![bin_name.to_string(), format!("./{0}", bin_name)];
if cfg!(windows) {
Expand Down Expand Up @@ -73,24 +74,27 @@ impl<'a, 'b> PowerShellGen<'a, 'b> {
}
}

fn generate_inner<'a, 'b>(p: &Parser<'a, 'b>, previous_command_name: &str) -> (String, String) {
fn generate_inner<'a, 'b, 'p>(p: &'p Parser<'a, 'b>, previous_command_name: &str, names: &mut Vec<&'p str>) -> (String, String) {
debugln!("PowerShellGen::generate_inner;");
let command_name = if previous_command_name.is_empty() {
format!("{}_{}", previous_command_name, &p.meta.bin_name.as_ref().expect(INTERNAL_ERROR_MSG))
} else {
format!("{}_{}", previous_command_name, &p.meta.name)
};

let mut subcommands_detection_cases = if previous_command_name == "" {
let mut subcommands_detection_cases = if previous_command_name.is_empty() {
String::new()
} else {
} else if !names.contains(&&*p.meta.name) {
names.push(&&*p.meta.name);
format!(r"
'{0}' {{
$command += '_{0}'
break
}}
",
&p.meta.name)
} else {
String::new()
};

let mut completions = String::new();
Expand All @@ -114,7 +118,7 @@ fn generate_inner<'a, 'b>(p: &Parser<'a, 'b>, previous_command_name: &str) -> (S

for subcommand in &p.subcommands {
let (subcommand_subcommands_detection_cases, subcommand_subcommands_cases) =
generate_inner(&subcommand.p, &command_name);
generate_inner(&subcommand.p, &command_name, names);
subcommands_detection_cases.push_str(&subcommand_subcommands_detection_cases);
subcommands_cases.push_str(&subcommand_subcommands_cases);
}
Expand Down

0 comments on commit 85b0e1c

Please sign in to comment.