From b5dd4f9123ae7464708bd84c76fd7f554bd350c2 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 15 Dec 2021 23:14:25 +0100 Subject: Add variable command options --- lua/rclone.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- plugin/rclone.vim | 5 +---- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lua/rclone.lua b/lua/rclone.lua index 54ae939..c584b6b 100644 --- a/lua/rclone.lua +++ b/lua/rclone.lua @@ -205,8 +205,8 @@ end -- 'rclone copy ' -- -- @return any -local function copy(remote_section) - local remote, config = get_remote_config(remote_section) +local function copy(options) + local remote, config = get_remote_config(options.remote) if remote == nil then return end @@ -233,8 +233,8 @@ end -- 'rclone copy ' -- -- @return any -local function copyFile(remote_section) - local remote, config = get_remote_config(remote_section) +local function copyFile(options) + local remote, config = get_remote_config(options.remote) if remote == nil then return end @@ -267,8 +267,8 @@ end -- 'rclone sync ' -- -- @return any -local function sync(remote_section) - local remote, config = get_remote_config(remote_section) +local function sync(options) + local remote, config = get_remote_config(options.remote) if remote == nil then return end @@ -290,9 +290,46 @@ local function sync(remote_section) return config end +-- Table for dynamic command access +local commands = { + copy = copy, + copyFile = copyFile, + sync = sync, +} + +--- +-- Parse args and run specified command +local function run(cmd, ...) + local args = { ... } + if cmd == nil then + print('No command supplied!') + return + end + + if commands[cmd] == nil then + print('Command "' .. cmd .. '" does not exist.') + return + end + + local options = {} + for _, arg in ipairs(args) do + if arg:find('%-%-') == 1 then + options[arg] = true + elseif arg:find('=', 1) == nil then + options['remote'] = arg + else + local param = vim.split(arg, '=') + local key = table.remove(param, 1) + param = table.concat(param, '=') + + options[key] = param + end + end + + commands[cmd](options) +end + return { - copy = copy, - copyFile = copyFile, - sync = sync, + run = run, } diff --git a/plugin/rclone.vim b/plugin/rclone.vim index 31c3f1b..568c839 100644 --- a/plugin/rclone.vim +++ b/plugin/rclone.vim @@ -5,10 +5,7 @@ endif let s:old_cpo = &cpo set cpo&vim -command! -nargs=? RcloneCopy lua require('rclone').copy() -command! -nargs=? RcloneCopyFile lua require('rclone').copyFile() -command! -nargs=? RcloneSync lua require('rclone').sync() - +command! -nargs=+ Rclone lua require('rclone').run(unpack({})) let &cpo = s:old_cpo unlet s:old_cpo -- cgit v1.2.3