src/cli/command.rs: cleanup, avoid array manipulation
This commit is contained in:
		@ -425,7 +425,7 @@ fn print_simple_completion(
 | 
				
			|||||||
    cli_cmd: &CliCommand,
 | 
					    cli_cmd: &CliCommand,
 | 
				
			||||||
    done: &mut HashSet<String>,
 | 
					    done: &mut HashSet<String>,
 | 
				
			||||||
    arg_param: &[&str],
 | 
					    arg_param: &[&str],
 | 
				
			||||||
    mut args: Vec<String>,
 | 
					    args: &[String],
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
    // fixme: arg_param, fixed_param
 | 
					    // fixme: arg_param, fixed_param
 | 
				
			||||||
    //eprintln!("COMPL: {:?} {:?} {}", arg_param, args, args.len());
 | 
					    //eprintln!("COMPL: {:?} {:?} {}", arg_param, args, args.len());
 | 
				
			||||||
@ -434,8 +434,7 @@ fn print_simple_completion(
 | 
				
			|||||||
        let prop_name = arg_param[0];
 | 
					        let prop_name = arg_param[0];
 | 
				
			||||||
        done.insert(prop_name.into());
 | 
					        done.insert(prop_name.into());
 | 
				
			||||||
        if args.len() > 1 {
 | 
					        if args.len() > 1 {
 | 
				
			||||||
            args.remove(0);
 | 
					            print_simple_completion(cli_cmd, done, &arg_param[1..], &args[1..]);
 | 
				
			||||||
            print_simple_completion(cli_cmd, done, &arg_param[1..], args);
 | 
					 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        } else if args.len() == 1 {
 | 
					        } else if args.len() == 1 {
 | 
				
			||||||
            if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
 | 
					            if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
 | 
				
			||||||
@ -448,11 +447,11 @@ fn print_simple_completion(
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    record_done_arguments(done, &cli_cmd.info.parameters, &args);
 | 
					    record_done_arguments(done, &cli_cmd.info.parameters, &args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let prefix = args.pop().unwrap(); // match on last arg
 | 
					    let prefix = &args[args.len()-1]; // match on last arg
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // complete option-name or option-value ?
 | 
					    // complete option-name or option-value ?
 | 
				
			||||||
    if !prefix.starts_with("-") && args.len() > 0 {
 | 
					    if !prefix.starts_with("-") && args.len() > 1 {
 | 
				
			||||||
        let last = &args[args.len()-1];
 | 
					        let last = &args[args.len()-2];
 | 
				
			||||||
        if last.starts_with("--") && last.len() > 2 {
 | 
					        if last.starts_with("--") && last.len() > 2 {
 | 
				
			||||||
            let prop_name = &last[2..];
 | 
					            let prop_name = &last[2..];
 | 
				
			||||||
            if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
 | 
					            if let Some((_, schema)) = cli_cmd.info.parameters.properties.get(prop_name) {
 | 
				
			||||||
@ -465,7 +464,7 @@ fn print_simple_completion(
 | 
				
			|||||||
    for (name, (_optional, _schema)) in &cli_cmd.info.parameters.properties {
 | 
					    for (name, (_optional, _schema)) in &cli_cmd.info.parameters.properties {
 | 
				
			||||||
        if done.contains(*name) { continue; }
 | 
					        if done.contains(*name) { continue; }
 | 
				
			||||||
        let option = String::from("--") + name;
 | 
					        let option = String::from("--") + name;
 | 
				
			||||||
        if option.starts_with(&prefix) {
 | 
					        if option.starts_with(prefix) {
 | 
				
			||||||
            println!("{}", option);
 | 
					            println!("{}", option);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -476,7 +475,7 @@ fn print_help_completion(def: &CommandLineInterface, help_cmd: &CliCommand, args
 | 
				
			|||||||
    match def {
 | 
					    match def {
 | 
				
			||||||
        CommandLineInterface::Simple(_) => {
 | 
					        CommandLineInterface::Simple(_) => {
 | 
				
			||||||
            let mut done = HashSet::new();
 | 
					            let mut done = HashSet::new();
 | 
				
			||||||
            print_simple_completion(help_cmd, &mut done, &help_cmd.arg_param, args.to_vec());
 | 
					            print_simple_completion(help_cmd, &mut done, &help_cmd.arg_param, args);
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        CommandLineInterface::Nested(map) => {
 | 
					        CommandLineInterface::Nested(map) => {
 | 
				
			||||||
@ -500,7 +499,7 @@ fn print_help_completion(def: &CommandLineInterface, help_cmd: &CliCommand, args
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn print_nested_completion(def: &CommandLineInterface, mut args: Vec<String>) {
 | 
					fn print_nested_completion(def: &CommandLineInterface, args: &[String]) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    match def {
 | 
					    match def {
 | 
				
			||||||
        CommandLineInterface::Simple(cli_cmd) => {
 | 
					        CommandLineInterface::Simple(cli_cmd) => {
 | 
				
			||||||
@ -517,13 +516,13 @@ fn print_nested_completion(def: &CommandLineInterface, mut args: Vec<String>) {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            let first = args.remove(0);
 | 
					            let first = &args[0];
 | 
				
			||||||
            if let Some(sub_cmd) = map.commands.get(&first) {
 | 
					            if let Some(sub_cmd) = map.commands.get(first) {
 | 
				
			||||||
                print_nested_completion(sub_cmd, args);
 | 
					                print_nested_completion(sub_cmd, &args[1..]);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            for cmd in map.commands.keys() {
 | 
					            for cmd in map.commands.keys() {
 | 
				
			||||||
                if cmd.starts_with(&first) {
 | 
					                if cmd.starts_with(first) {
 | 
				
			||||||
                    println!("{}", cmd);
 | 
					                    println!("{}", cmd);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
@ -564,7 +563,7 @@ pub fn print_bash_completion(def: &CommandLineInterface) {
 | 
				
			|||||||
    if !args.is_empty() && args[0] == "help" {
 | 
					    if !args.is_empty() && args[0] == "help" {
 | 
				
			||||||
        print_help_completion(def, &help_command_def(), &args[1..]);
 | 
					        print_help_completion(def, &help_command_def(), &args[1..]);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        print_nested_completion(def, args);
 | 
					        print_nested_completion(def, &args);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user