Added help and exit
This commit is contained in:
@@ -1,5 +1,33 @@
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
|
||||
def main(args):
|
||||
for item in args:
|
||||
Path(args).unlink(missing_ok=True) # missing_ok=True prevents error if file is missing
|
||||
if not args:
|
||||
print("remove: no arguments were given")
|
||||
return
|
||||
|
||||
# Check for the -rf flag
|
||||
recursive = False
|
||||
if "-rf" in args:
|
||||
recursive = True
|
||||
args.remove("-rf") # Remove flag from list so only paths remain
|
||||
|
||||
if args == ["-h"] or args == ["--help"]:
|
||||
print("remove: Use -rf to delete directories and their contents.")
|
||||
else:
|
||||
for item in args:
|
||||
path = Path(item)
|
||||
|
||||
if not path.exists():
|
||||
continue # 'force' usually means ignore non-existent files
|
||||
|
||||
try:
|
||||
if path.is_dir():
|
||||
if recursive:
|
||||
shutil.rmtree(path) # This is the "recursive" part
|
||||
else:
|
||||
print(f"remove: cannot remove '{item}': Is a directory")
|
||||
else:
|
||||
path.unlink() # Delete individual file
|
||||
except Exception as e:
|
||||
print(f"remove: error deleting {item}: {e}")
|
||||
Reference in New Issue
Block a user