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

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( "tapered" )
local rmcmdline = require( "rmcmdline" )

-- should work for unix
t.is( "rm", rmcmdline( { unix = true } ) )
t.is( "rm -f", rmcmdline( { unix = true }, { force = true } ) )
t.is( "rm -r -f", rmcmdline( { unix = true }, { recursive = true, force = true } ) )

-- should work for windows
t.is( "del", rmcmdline( { windows = true } ) )
t.is( "del /q", rmcmdline( { windows = true }, { force = true } ) )
t.is( "rd /s /q", rmcmdline( { windows = true }, { recursive = true, force = true } ) )

t.done()

See also