how to "copy" existing completions to other commands in zsh - autocomplete

I have a custom script that takes hostnames as parameters. I know that I can easily copy the existing completion of ssh like this:
compdef myscript=ssh
But that only enables completion of the 1st parameter. Is there an easy way to enable the same completion for all parameters?

I'm not aware of an easy method to enable completion for a custom command. Assuming you've got a command foo with a bunch of allowable arguments bar, bas or baz, then the completion is easy: you can either have foo bar, foo bas, or foo baz. If they're not or'd, though, you could have any combination of the three.
It gets somewhat worse when you've got a 'depth' of more than 1 (bar can take arguments car, cas and caz, for example).
In zsh, my general understanding is that completion for commands is detailed in completion functions. These functions are application specific, because the arguments for each application are specific to those applications. As an example, the tmux (a terminal multiplexer, similar to screen, in case you're not familiar) has a completion function that's fairly complex: here's a link.
If you want to write your own completion functions, the documentation is available and accessible. Here are a bunch of links that I'm (slowly) working my way through - they'll definitely tell you how to get completion working, but there's a lot of reading. The Z-Shell is a complex beast.
Z-Shell completion introduction
Z-Shell functions: Writing and loading your own
ZSH Users Guide, Ch. 6: "Completion, old and new"
You're specifically interested in enabling completion for hostname-like arguments, and you've singled out ssh as the idea. The zsh completion function for ssh is defined in Completion/Unix/Command/_ssh, if you've got the ZSH source. If not, here's a link.
Of course, this SO question is rather similar. compdef alone may do what you want, if myscript and ssh parameters are identical enough.

Related

How to find the parameters for a VSCode command in executeCommands()

I want to write a vscode extension and use vscode.commands.executeCommands(), but I don't know what parameters the command I want to use takes.
For example, when I want to use the "actions.find" command, how do I find out what parameters this specific API accepts?
I don't think there's any comprehensive documentation on commands and their arguments at this time. This page does list some of the most important ones though.
For some commands, there's also args auto-completion in keybindings.json:
Note that a lot of the built-in commands aren't much of an "API" at all. Looking at its implementation, "actions.find" in particular does not seem to support any arguments that would be useful to extensions. Compare this to the implementation of "workbench.action.findInFiles", which supports a well-defined set of arguments such as search query etc.

What fish function controls completion of filenames, and how can I change it?

I am trying to alter the standard logic used by fish to find filename completions. In particular, I want fish not to consider any filename that ends in a tilde (~) character, as these are emacs backup files and are not interesting.
I had assumed that the list of possible completions would be provided by a fish function, which I could then edit to remove the ones ending in tildes. But I cannot find the function. I have looked in the documentation at fishshell.com, and I have also tried functions | grep complete. What function should I be editing?
Unlike most completions file name completions is not implemented as a function; it's baked into the C++ code. See the completer_t::complete_param_expand() method in src/complete.cpp. If you can make a cogent argument for how this type of customization would be implemented I'd encourage you to open an issue.
P.S., Note that functions will not show private functions unless you invoke it as functions -a. I mention this because many completion functions are marked private by beginning their name with an underscore.

How do I find out which function is used for zsh tab completion

I want to know which function zsh is using for tab completion of a command.
For many commands (make, ls, cd …) I can apparently guess _<COMMANDNAME>, but I might actually have overriden this setting with compdef _mycd cd.
The reasons why I want to know this are two:
I might want to read some function definitions to use parts of them in functions I'm writing
I want to wrote a completion function which itself calls the completion for other commands (like one does in _nice, with the exception that I might not rely on shift; CURRENT--; _normal)
The current completion rules are stored in $_comps.
So one can display the completion rule for cd with echo $_comps[cd] and display the actual function definition with functions $_comps[cd].

auto completion in zsh on 3rd parameter

I have a shell script it's usage is:
foo.sh <name> <type> <*.tar.gz>
I want to setup a complete on 3rd parameter only.
If I press on 1st parameter, just show the usage.
Could I use zsh's zshcomp to do this job?
for example:
foo.sh <tab> # display usage
foo.sh a b <tab> # show files match with *.tar.gz
Is there similar script which I could follow?
Things to read.
Here is a blog post discussing the Z-Shell Completion System.
For a slightly more in-depth discussion, read this unix.stackexchange answer.
And, as always, read the man pages!.
Edit: Forgot to add: echo $fpath will show you the function path that zsh uses. On OSX, I have: /usr/local/share/zsh/4.3.17/functions, (location may vary for you), which contains all the ZSH completion functions. Have a look at _ssh, _ls, _tar etc - they're all there, and they all have lots of nifty features you can learn from.
Addressing the question: the direction you should go.
What you're asking is achievable, though. There are several steps.
You need to write a z-shell completion function. It needs to be located on the fpath; the function-path that zsh uses for it's completion system functions. (If it's a small function, putting it into ~/.zshrc will also work, but isn't recommended).
You want completion on the 3rd parameter. To do that, your function would look something like the following:
_arguments "3:<MENU>:<COMPLETION>"
<MENU> is the menu description, which you'll see if you've enabled the menu descriptions. (That's done using zstyle; read the man pages, or the linked pages, for more information).
<COMPLETION> is the things that you can complete with. For example, if you used:
_arguments "3::(Foo Bar)"
when you ran your script and pressed <TAB>, you'd have the option of either Foo or Bar.
[NOTE: There is no <MENU> in that example. If you don't want a menu descriptor, you can omit it as shown].
You want completion on *.tar files. You can call _files to do that:
_files -g \*.tar
Show usage on first parameter: that'd be a completion with no arguments (ie, argument 1). I'm not sure why you'd want completion on only the third option but not the first two (typical usage descriptions), so I don't know how to answer this. If your script is supposed to be used like this: script foo bar FILE.tar, wouldn't you want to complete the foo and bar arguments as well?
Perhaps a better solution would be displaying the usage when the script is run without parameters.
Full script would look something like the following:
#compdef myscript
_arguments "3:tar files:_files -g \*.tar"

Argument passing strategy - environment variables vs. command line

Most of the applications we developers write need to be externally parametrized at startup. We pass file paths, pipe names, TCP/IP addresses etc. So far I've been using command line to pass these to the appplication being launched. I had to parse the command line in main and direct the arguments to where they're needed, which is of course a good design, but is hard to maintain for a large number of arguments. Recently I've decided to use the environment variables mechanism. They are global and accessible from anywhere, which is less elegant from architectural point of view, but limits the amount of code.
These are my first (and possibly quite shallow) impressions on both strategies but I'd like to hear opinions of more experienced developers -- What are the ups and downs of using environment variables and command line arguments to pass arguments to a process? I'd like to take into account the following matters:
design quality (flexibility/maintainability),
memory constraints,
solution portability.
Remarks:
Ad. 1. This is the main aspect I'm interested in.
Ad. 2. This is a bit pragmatic. I know of some limitations on Windows which are currently huge (over 32kB for both command line and environment block). I guess this is not an issue though, since you just should use a file to pass tons of arguments if you need.
Ad. 3. I know almost nothing of Unix so I'm not sure whether both strategies are as similarily usable as on Windows. Elaborate on this if you please.
1) I would recommend avoiding environmental variables as much as possible.
Pros of environmental variables
easy to use because they're visible from anywhere. If lots of independent programs need a piece of information, this approach is a whole lot more convenient.
Cons of environmental variables
hard to use correctly because they're visible (delete-able, set-able) from anywhere. If I install a new program that relies on environmental variables, are they going to stomp on my existing ones? Did I inadvertently screw up my environmental variables when I was monkeying around yesterday?
My opinion
use command-line arguments for those arguments which are most likely to be different for each individual invocation of the program (i.e. n for a program which calculates n!)
use config files for arguments which a user might reasonably want to change, but not very often (i.e. display size when the window pops up)
use environmental variables sparingly -- preferably only for arguments which are expected not to change (i.e. the location of the Python interpreter)
your point They are global and accessible from anywhere, which is less elegant from architectural point of view, but limits the amount of code reminds me of justifications for the use of global variables ;)
My scars from experiencing first-hand the horrors of environmental variable overuse
two programs we need at work, which can't run on the same computer at the same time due to environmental clashes
multiple versions of programs with the same name but different bugs -- brought an entire workshop to its knees for hours because the location of the program was pulled from the environment, and was (silently, subtly) wrong.
2) Limits
If I were pushing the limits of either what the command line can hold, or what the environment can handle, I would refactor immediately.
I've used JSON in the past for a command-line application which needed a lot of parameters. It was very convenient to be able to use dictionaries and lists, along with strings and numbers. The application only took a couple of command line args, one of which was the location of the JSON file.
Advantages of this approach
didn't have to write a lot of (painful) code to interact with a CLI library -- it can be a pain to get many of the common libraries to enforce complicated constraints (by 'complicated' I mean more complex than checking for a specific key or alternation between a set of keys)
don't have to worry about the CLI libraries requirements for order of arguments -- just use a JSON object!
easy to represent complicated data (answering What won't fit into command line parameters?) such as lists
easy to use the data from other applications -- both to create and to parse programmatically
easy to accommodate future extensions
Note: I want to distinguish this from the .config-file approach -- this is not for storing user configuration. Maybe I should call this the 'command-line parameter-file' approach, because I use it for a program that needs lots of values that don't fit well on the command line.
3) Solution portability: I don't know a whole lot about the differences between Mac, PC, and Linux with regard to environmental variables and command line arguments, but I can tell you:
all three have support for environmental variables
they all support command line arguments
Yes, I know -- it wasn't very helpful. I'm sorry. But the key point is that you can expect a reasonable solution to be portable, although you would definitely want to verify this for your programs (for example, are command line args case sensitive on any platforms? on all platforms? I don't know).
One last point:
As Tomasz mentioned, it shouldn't matter to most of the application where the parameters came from.
You should abstract reading parameters using Strategy pattern. Create an abstraction named ConfigurationSource having readConfig(key) -> value method (or returning some Configuration object/structure) with following implementations:
CommandLineConfigurationSource
EnvironmentVariableConfigurationSource
WindowsFileConfigurationSource - loading from a configuration file from C:/Document and settings...
WindowsRegistryConfigurationSource
NetworkConfigrationSource
UnixFileConfigurationSource - - loading from a configuration file from /home/user/...
DefaultConfigurationSource - defaults
...
You can also use Chain of responsibility pattern to chain sources in various configurations like: if command line argument is not supplied, try environment variable and if everything else fails, return defauls.
Ad 1. This approach not only allows you to abstract reading configuration, but you can easily change the underlying mechanism without any affect on client code. Also you can use several sources at once, falling back or gathering configuration from different sources.
Ad 2. Just choose whichever implementation is suitable. Of course some configuration entries won't fit for instance into command line arguments.
Ad 3. If some implementations aren't portable, have two, one silently ignored/skipped when not suitable for a given system.
I think this question has been answered rather well already, but I feel like it deserves a 2018 update. I feel like an unmentioned benefit of environmental variables is that they generally require less boiler plate code to work with. This makes for cleaner more readable code. However a major disadvatnage is that they remove a layers of isolation from different applications running on the same machine. I think this is where Docker really shines. My favorite design pattern is to exclusively use environment variables and run the application inside of a Docker container. This removes the isolation issue.
I generally agree with previous answers, but there is another important aspect: usability.
For example, in git you can create a repository with the .git directory outside of that. To specify that, you can use a command line argument --git-dir or an environmental variable GIT_DIR.
Of course, if you change the current directory to another repository or inherit environmental variables in scripts, you get a mistake. But if you need to type several git commands in a detached repository in one terminal session, this is extremely handy: you don't need to repeat the git-dir argument.
Another example is GIT_AUTHOR_NAME. It seems that it even doesn't have a command line partner (however, git commit has an --author argument). GIT_AUTHOR_NAME overrides the user.name and author.name configuration settings.
In general, usage of command line or environmental arguments is equally simple on UNIX: one can use a command line argument
$ command --arg=myarg
or an environmental variable in one line:
$ ARG=myarg command
It is also easy to capture command line arguments in an alias:
alias cfg='git --git-dir=$HOME/.cfg/ --work-tree=$HOME' # for dotfiles
alias grep='grep --color=auto'
In general most arguments are passed through the command line. I agree with the previous answers that this is more functional and direct, and that environmental variables in scripts are like global variables in programs.
GNU libc says this:
The argv mechanism is typically used to pass command-line arguments specific to the particular program being invoked. The environment, on the other hand, keeps track of information that is shared by many programs, changes infrequently, and that is less frequently used.
Apart from what was said about dangers of environmental variables, there are good use cases of them. GNU make has a very flexible handling of environmental variables (and thus is very integrated with shell):
Every environment variable that make sees when it starts up is transformed into a make variable with the same name and value. However, an explicit assignment in the makefile, or with a command argument, overrides the environment. (-- and there is an option to change this behaviour) ...
Thus, by setting the variable CFLAGS in your environment, you can cause all C compilations in most makefiles to use the compiler switches you prefer. This is safe for variables with standard or conventional meanings because you know that no makefile will use them for other things.
Finally, I would stress that the most important for a program is not programmer, but user experience. Maybe you included that into the design aspect, but internal and external design are pretty different entities.
And a few words about programming aspects. You didn't write what language you use, but let's imagine your tools allow you the best possible argument parsing. In Python I use argparse, which is very flexible and rich. To get the parsed arguments, one can use a command like
args = parser.parse_args()
args can be further split into parsed arguments (say args.my_option), but I can also pass them as a whole to my function. This solution is absolutely not "hard to maintain for a large number of arguments" (if your language allows that). Indeed, if you have many parameters and they are not used during argument parsing, pass them in a container to their final destination and avoid code duplication (which leads to inflexibility).
And the very final comment is that it's much easier to parse environmental variables than command line arguments. An environmental variable is simply a pair, VARIABLE=value. Command line arguments can be much more complicated: they can be positional or keyword arguments, or subcommands (like git push). They can capture zero or several values (recall the command echo and flags like -vvv). See argparse for more examples.
And one more thing. Your worrying about memory is a bit disturbing. Don't write overgeneral programs. A library should be flexible, but a good program is useful without any arguments. If you need to pass a lot, this is probably data, not arguments. How to read data into a program is a much more general question with no single solution for all cases.