If you want to customize the completion on a per-script basis, then one way to approach this problem would be to use bash_completion with a custom completion function. This function could parse the given perl script for completion hints which could be passed to compgen (a bash builtin).
I haven't worked with bash_completion enough to provide a working example unfortunately. One potential shortcoming would be that you need to tell bash when to run this completion code. This is typically done by specifying the completion logic for a given command. (e.g. perl is handled by the _perl function).
Example code with completion config in comments
#!/usr/bin/perl
#complete -G '*.bak'
And awk to extract it:
awk 'NR == 1 {next} /^\#complete/ {print} /^[^#]/ {exit}' $filename
Explanation of the awk:
- NR == 1 {next}
Skip the first record
- /^\#complete/ {print}
Print any lines that start with #complete
- /^^#/ {exit}
Stop processing when a non-comment line is reached
|