Is it possible to pass arguments to Vimscript functions with the --argument or -a format, similar to arguments in the command line?

I am writing a command that submits a job in a remote machine. I have to pass the number of hours and the number of CPUs requested. Currently they are provided as positional parameters.

function! SubmitJob(hours,cpus)
com -nargs=* Submit call SubmitJob(<f-args>)

For example, :Submit 1 2 indicates 1 hour and 2 CPUs. Would it be possible to change the function and command above so that I can pass the arguments as :Submit --hours 1 --cpus 2 or :Submit -h 1 -c 2? This way, it would be possible to change the parameter order, for example :Submit --cpus 2 --hours 1.

1 Answer 1


No, you have to write such "option parser" yourself.

But quite probably there's no need to do this. Just re-write your function to accept dictionary argument. For example,

function! SubmitJob(opts) abort
    let l:opts = extend(copy(a:opts), #{hours: 1, cpus: 1}, 'keep')
    echo l:opts.hours l:opts.cpus

:call SubmitJob(#{cpus: 2})
" hours=1 cpus=2
  • Thanks! Would it be possible to make a command for this function? Would the regular command syntax com -nargs=* Submit call SubmitJob(<f-args>) also work for functions with dictionary arguments?
    – mgarort
    Commented May 7, 2021 at 3:44
  • 1
    @mgarort command ... call SubmitJob(ParseAndBuildDictionary(<f-args>))
    – Matt
    Commented May 7, 2021 at 5:46
  • Sorry for my ignorance, but I understand that ParseAndBuildDictionary is a custom function? I don't find it in VIM - Vi IMproved 8.0
    – mgarort
    Commented May 7, 2021 at 16:11
  • @mgarort Yes, (almost) all builtin commands/functions start with a lower case letter. As I already said in my answer "you have to write option parser yourself".
    – Matt
    Commented May 7, 2021 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.