I am working on a VIM plugin that will require a certain level of complexity for handling arguments passed to a command.
:MyCommand -item=banana -command=ls\-ltra -pattern=./*
However, I am not sure how to handle this situation. I am aware of how arguments work in vimL, with the a:0
and a:000
and the <f-args>
bits. But not sure how to do the splitting and filtering of the strings etc.
Ideally, I will be constructing a dictionary out of my arguments:
{
'item':'banana',
'command':'ls -ltra',
'pattern':'./*',
}
Of course, there are other plugins out there that I can peak into and see how they have solved this issue to certain extend, yet still, any suggestions would be at a great help.
--- EDIT(1) ---
Here is a prototype:
function! MyCommand(...) abort
" Total argument count.
echo 'a:0 -> ' . a:0
" Iterative lookup.
" echo 'a:1 -> ' . a:1
" echo 'a:2 -> ' . a:2
" ... etc.
let l:limit = a:0
let l:counter = 1
while l:limit >= l:counter
exec 'echo ' . '"a:' . l:counter . '"' . '"->"' . 'a:' . l:counter
let l:counter += 1
endwhile
" All arguments as a list.
echo 'a:000:'
echo a:000
endfunction
And this when run as:
:MyCommand -stage=build -task=do -command=ls\-ltr -pattern=./* -working-directory=/path/to/my/project
Produces the following output:
a:0 -> 5
a:1 -> -stage=build
a:2 -> -task=do
a:3 -> -command=ls\-ltr
a:4 -> -pattern=./*
a:5 -> -working-directory=/path/to/my/project
a:000:
['-stage=build', '-task=do', '-command=ls\-ltr', '-pattern=./*', 'working-directory=/path/to/my/project']
So, I guess, the rest would be regex based checks and string processing to extract the right bits of information. Anything more efficient that should be done on the function and arguments handling side?