argh icon indicating copy to clipboard operation
argh copied to clipboard

`Complete` subcommand to list all subcommands for a given argument.

Open naudzghebre opened this issue 2 years ago • 1 comments

Adding a subcommand for all commands, similar to help, which is responsible for generating all the subcommands for a given command.

This function can be used to assist with auto-completion in the shell by providing the set of possible subcommands available at the cursor.

naudzghebre avatar Jul 25 '22 22:07 naudzghebre

Could you go into some more detail about why building this functionality into argh is advantageous, vs the clients implementing it on their own? Extending from my example, it wouldn't be hard to do:

let cmd: MyCmd = argh::from_env();
if cmd.completions {
    for subcommand in MyCmd::COMMANDS.iter().chain(MyCmd::dynamic_commands()) {
        println!("{:?}", cmd.name);
    }
    return;
}

My thinking was implementing it in argh would be most straightforward/simplest, since it seems that's where relative structure between commands and subcommands is accessible (e.g. help() gathering all of the info about a specific command). Truth be told, I didn't consider client side in this way. This approach seems like a large amount of changes would have to be made to each of the existing command structs. And would we have to enforce any new command implementations to have a complete switch added in order to get an accurate representation?

The main downside I can see is that it's not recursive. However, if we land #119 then we'd have a way to walk through the full recursive argument structure.

If https://github.com/google/argh/pull/119 is landed, does that mean we'd have a way to traverse the entire topology of the argument parser's final output? This is actually an approach I wanted to take. Something to the effect of building out and writing the entire structure to some intermediate schema that can be parsed by shell-specific scripts implemented by the user.

One level beyond this would be to spit out the shell specific scripts from within argh, just as you suggested. But I think we'd need this ability of exposing all of the possible command/subcommands/options to do this.

And good point about the backwards incompatibility. I think this is going to need iterating.

naudzghebre avatar Jul 27 '22 19:07 naudzghebre