Skip to content

Commit

Permalink
feat: Detect and merge subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Aug 9, 2023
1 parent 89aaed1 commit 33c8ed5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/gen/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ fn generate_fn(

if !cmd_info.subcommands.is_empty() {
out.push_str(&format!("{INDENT}case $line[{}] in\n", pos + 1));
for _sub_cmd in cmd_info.subcommands.keys() {
todo!()
for sub_cmd in cmd_info.subcommands.keys() {
out.push_str(&format!("{INDENT}{INDENT}{sub_cmd}) {sub_cmd};;"))
}
out.push_str(&format!("{INDENT}esac\n"));
}
Expand Down
36 changes: 25 additions & 11 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use flate2::bufread::GzDecoder;
use log::{debug, error, trace, warn};
use regex::Regex;
use std::{
collections::{HashMap, HashSet},
collections::{hash_map::Entry, HashMap, HashSet},
fs::File,
io::{BufReader, Read},
path::{Path, PathBuf},
Expand Down Expand Up @@ -181,6 +181,26 @@ impl ManParseConfig {
}
}

fn insert_cmd(
subcommands: &mut HashMap<String, CommandInfo>,
mut cmd_parts: Vec<String>,
mut args: Vec<Arg>,
) {
let head = cmd_parts.remove(0);
let cmd = match subcommands.entry(head) {
Entry::Occupied(o) => o.into_mut(),
Entry::Vacant(v) => v.insert(CommandInfo {
args: Vec::new(),
subcommands: HashMap::new(),
}),
};
if cmd_parts.is_empty() {
cmd.args.append(&mut args);
} else {
insert_cmd(&mut cmd.subcommands, cmd_parts, args);
}
}

fn parse_all_manpages(manpages: Vec<(String, PathBuf)>) -> HashMap<String, CommandInfo> {
let mut res = HashMap::new();

Expand All @@ -190,14 +210,10 @@ fn parse_all_manpages(manpages: Vec<(String, PathBuf)>) -> HashMap<String, Comma
match parse_manpage_text(&cmd_name, &text) {
Some(parsed) => {
trace!("Parsing man page for {} at {}", cmd_name, manpage.display());
// todo merge subcommands later instead
res.insert(
cmd_name,
CommandInfo {
args: parsed,
subcommands: HashMap::new(),
},
);
match detect_subcommand(&cmd_name, &text) {
Some(cmd_parts) => insert_cmd(&mut res, cmd_parts, parsed),
None => insert_cmd(&mut res, vec![cmd_name], parsed),
}
}
None => {
error!("Could not parse manpage for {}", cmd_name);
Expand All @@ -212,8 +228,6 @@ fn parse_all_manpages(manpages: Vec<(String, PathBuf)>) -> HashMap<String, Comma
}
}

// TODO merge subcommands

res
}

Expand Down
33 changes: 0 additions & 33 deletions tests/resources/expected/_git.zsh

This file was deleted.

0 comments on commit 33c8ed5

Please sign in to comment.