Hacker News new | ask | show | jobs
by shagie 674 days ago
How would a shell interrogate the valid parameters from a complied binary?

I know that approaches like Powershell which has understanding of all the Microsoft commands and the ability to define a structure that defines the options ... but how would bash figure out the structure of the arguments for dd or tar or that a.out that I just compiled?

2 comments

By accompanying the binary with a formal description of the possible arguments, either as a separate file or embedded in the executable. But the latter is not really sustainable the more metadata you add, so more likely a separate file.

Applications already come with lots of metadata: man pages, icon, readable name, ... adding formal shell-completion information is just another one.

Besides that, the built-in command line tools already have auto-completion for their arguments. Though I think they do it by running scripts at shell startup, which isn't exactly the mechanism I would have used... but it works.

In your case, "that a.out I just compiled", this metadata would be missing, so no shell completion for you. But that is a niche use case anyway, and not at all what this post is about.

I'm kinda surprised this is news to readers here.
The default shell on debian autocompletes git commands and things like branch names pretty robustly for me, so somebody figured it out.
That works in similar ways as posh-git ( https://github.com/dahlbyk/posh-git ) - the tooling for Powershell to be able to be able to do things with git.

This has a fair bit of code that goes in and examines the .git directory when present.

Additionally, the shell has been programed to be aware of the git sub commands.

But is it aware of... say... svn sub commands? The same amount of effort would be need to be done to have bash aware of .svn and its sub commands.

This is special tooling written for one suite of commands.

It doesn't work for arbitrary commands or arbitrary data structures. Even well known ones.

    tar -zxvf <tar filename> {specific file}
That doesn't do tab expansion for the specific files that are within the tar file. Nor does doing --f show the expansion options for --file --format --fflags

Someone wrote special case tooling for one shell for one command named git.