From 7aa4801c83d06ac5285acdc15ed6758c709df481 Mon Sep 17 00:00:00 2001 From: ysthakur <45539777+ysthakur@users.noreply.github.com> Date: Fri, 18 Aug 2023 22:58:00 -0400 Subject: [PATCH] fix: Nested subcommands --- README.md | 1 - src/gen/nu.rs | 7 +------ src/gen/zsh.rs | 15 ++++----------- tests/resources/expected/_test1.bash | 11 ++++++++++- tests/resources/expected/_test1.zsh | 15 ++++++++++++++- tests/resources/expected/test1.nu | 5 +++++ tests/resources/in/man1/test1-sub1-nested.1 | 17 +++++++++++++++++ 7 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 tests/resources/in/man1/test1-sub1-nested.1 diff --git a/README.md b/README.md index 7f74539..1579bdc 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,6 @@ Things to do: - Port darwin and degroff parsers - Test the type 4 parser - Find samples of type 4, Darwin, and Degroff to test -- Ensure nested subcommands work - Add .gz files to the tests folder - Test excluding/including commands and directories - Figure out why fish only seems to use man1, man6, and man8 diff --git a/src/gen/nu.rs b/src/gen/nu.rs index a084e4e..28e3c53 100644 --- a/src/gen/nu.rs +++ b/src/gen/nu.rs @@ -58,11 +58,6 @@ fn generate_cmd( out.writeln("]"); for sub_cmd in &cmd.subcommands { - generate_cmd( - &format!("{} {}", cmd.name, sub_cmd.name), - sub_cmd, - out, - false, - ); + generate_cmd(&format!("{cmd_name} {}", sub_cmd.name), sub_cmd, out, false); } } diff --git a/src/gen/zsh.rs b/src/gen/zsh.rs index f065703..4fc397c 100644 --- a/src/gen/zsh.rs +++ b/src/gen/zsh.rs @@ -41,7 +41,7 @@ pub fn generate(cmd: &CommandInfo, out_dir: &Path) -> Result<()> { let comp_name = format!("_{}", cmd.name); let mut res = Output::new(String::from("\t")); res.writeln(format!("#compdef {comp_name} {}", cmd.name)); - generate_fn(cmd, &mut res, 0, &comp_name); + generate_fn(cmd, &mut res, &comp_name); fs::write(out_dir.join(format!("{comp_name}.zsh")), res.text())?; Ok(()) } @@ -49,12 +49,10 @@ pub fn generate(cmd: &CommandInfo, out_dir: &Path) -> Result<()> { /// Generate a completion function for a command/subcommand /// /// ## Arguments -/// * `pos` - If this is a top-level command, 0. Otherwise, if this is a -/// subcommand, which argument number the subcommand is (how deep it is) /// * `fn` - What to name the completion function. If you have a command `foo` /// with subcommand `bar`, the completion function for `foo bar` would be /// named `_foo_bar` -fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) { +fn generate_fn(cmd: &CommandInfo, out: &mut Output, fn_name: &str) { out.writeln(""); out.writeln(format!("function {fn_name} {{")); out.indent(); @@ -97,7 +95,7 @@ fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) { out.writeln("'*::arg:->args'"); out.dedent(); - out.writeln(format!("case $line[{}] in", pos + 1)); + out.writeln("case $line[1] in"); out.indent(); for sub_cmd in &cmd.subcommands { out.writeln(format!("{}) {fn_name}_{};;", sub_cmd.name, sub_cmd.name)); @@ -110,11 +108,6 @@ fn generate_fn(cmd: &CommandInfo, out: &mut Output, pos: usize, fn_name: &str) { out.writeln("}"); for sub_cmd in &cmd.subcommands { - generate_fn( - sub_cmd, - out, - pos + 1, - &format!("{fn_name}_{}", sub_cmd.name), - ); + generate_fn(sub_cmd, out, &format!("{fn_name}_{}", sub_cmd.name)); } } diff --git a/tests/resources/expected/_test1.bash b/tests/resources/expected/_test1.bash index cb21b53..cad9705 100644 --- a/tests/resources/expected/_test1.bash +++ b/tests/resources/expected/_test1.bash @@ -8,7 +8,16 @@ function _comp_cmd_test1 { case ${COMP_WORDS[1]} in sub1) case $COMP_CWORD in - 2) COMPREPLY=($(compgen -W '--foobar' -- $2)) ;; + 2) COMPREPLY=($(compgen -W '--foobar nested' -- $2)) ;; + *) + case ${COMP_WORDS[2]} in + nested) + case $COMP_CWORD in + 3) COMPREPLY=($(compgen -W '-c --command --install' -- $2)) ;; + esac + ;; + esac + ;; esac ;; sub2) diff --git a/tests/resources/expected/_test1.zsh b/tests/resources/expected/_test1.zsh index 599c979..7c7d954 100644 --- a/tests/resources/expected/_test1.zsh +++ b/tests/resources/expected/_test1.zsh @@ -17,8 +17,21 @@ function _test1 { } function _test1_sub1 { + local line + _arguments -C \ + '--foobar[Something something]' \ + ': :(nested)' \ + '*::arg:->args' + case $line[1] in + nested) _test1_sub1_nested;; + esac +} + +function _test1_sub1_nested { _arguments \ - '--foobar[Something something]' + '-c[Run a command or something]' \ + '--command[Run a command or something]' \ + '--install[Install a thing]' } function _test1_sub2 { diff --git a/tests/resources/expected/test1.nu b/tests/resources/expected/test1.nu index b2125cd..94a67f3 100644 --- a/tests/resources/expected/test1.nu +++ b/tests/resources/expected/test1.nu @@ -8,6 +8,11 @@ export extern "test1 sub1" [ --foobar # Something something ] +export extern "test1 sub1 nested" [ + --command(-c) # Run a command or something + --install # Install a thing +] + export extern "test1 sub2" [ --a # Both options should be picked up even though the short one is weird --all # Both options should be picked up even though the short one is weird diff --git a/tests/resources/in/man1/test1-sub1-nested.1 b/tests/resources/in/man1/test1-sub1-nested.1 new file mode 100644 index 0000000..c61ccbf --- /dev/null +++ b/tests/resources/in/man1/test1-sub1-nested.1 @@ -0,0 +1,17 @@ +THIS IS JUST FOR TESTING, NOT A REAL MAN PAGE + +This corresponds to Fish's type 3 and is a subcommand of test1 sub1 + +test1 sub1 nested <- Need that for this to be picked up as a subcommand + +.SH DESCRIPTION +-bleh bleh bleh +bleh bleh +.TP +\-c, \-\-command +Run a command or something +.HP +\-\-install +.IP +Install a thing +.SH Next section