rmcmdline

function rmcmdline( system [, options] ) --> cmdline

Description

Creates a correct command to remove a file or a folder.

Parameters

system

A sysinfo table.

options

The options for the command.

  • force

  • recursive

Return Values

cmdline

A valid command string.

Code

--ZFUNC-rmcmdline-v1
local function rmcmdline( system, options ) --> cmdline
   local exec_unix = function( options )
      local cmd = {}
      table.insert( cmd, "rm" )
      if options.recursive then table.insert( cmd, "-r" ) end
      if options.force then table.insert( cmd, "-f" ) end
      return table.concat( cmd, " " )
   end

   local exec_win = function( options, files )
      local cmd = {}
      if options.recursive then
         table.insert( cmd, "rd" )
         table.insert( cmd, "/s" )
      else
         table.insert( cmd, "del" )
      end
      if options.force then table.insert( cmd, "/q" ) end
      return table.concat( cmd, " " )
   end

   options = options or {}

   if system.unix then
      return exec_unix( options )
   elseif system.windows then
      return exec_win( options )
   end

   return nil
end

return rmcmdline

Examples

local t = require( "taptest" )
local rmcmdline = require( "rmcmdline" )

-- should work for unix
cmd = rmcmdline( { unix = true } )
t( cmd, "rm" )

cmd = rmcmdline( { unix = true }, { force = true } )
t( cmd, "rm -f" )

cmd = rmcmdline( { unix = true }, { recursive = true, force = true } )
t( cmd, "rm -r -f" )

-- should work for windows
cmd = rmcmdline( { windows = true } )
t( cmd, "del" )

cmd = rmcmdline( { windows = true }, { force = true } )
t( cmd, "del /q" )

cmd = rmcmdline( { windows = true }, { recursive = true, force = true } )
t( cmd, "rd /s /q" )

t()

See also