def rm(args): """ provide a argparse namespace object. Calls the rm command """ commands.rm(args.date)
def test_command_rm(monkeypatch): home = Folder('home') working_dir = Folder('working_dir') working_dir = home folder1 = Folder('folder1', home) file1 = File(home, 'file1', 'txt', 1000) assert len(home.content) == 2 input_decision = StringIO('Y\n') monkeypatch.setattr('sys.stdin', input_decision) rm(file1, working_dir, home) assert len(home.content) == 1
def do_rm(self, args): 'Usage: rm [-r] NODE...\n' \ 'Removes given NODE(s), -r option must be given if at least one\n' \ 'of them is group\n' args = parse(args) if "-r" in args: recursive = True args.remove('-r') else: recursive = False args = [self.path + node_path if node_path[0] != '/' else node_path for node_path in args] if not len(args): args.append(self.path) result = commands.rm(args, recursive) print(result)
def main(): home = Folder('home') working_dir = Folder('working_dir') working_dir = home answer = True starting_msg = "File system is working. Type help if needed or exit to end program." print(starting_msg) while answer: try: path = get_path(working_dir, home) answer = input(path).split() if answer[0] == "cd": new_working_dir = cd(answer, working_dir, home) if new_working_dir: working_dir = new_working_dir else: print("Incorrect usage of: cd. Try again") elif answer[0] == 'mkdir': # make folder try: Folder(answer[1], working_dir) except Exception: print("Incorrect usage of: mkdir. Try again") elif answer[0] == 'mk': # make file try: File(working_dir, answer[1], answer[2], answer[3]) except Exception: print("Incorrect usage of: mk. Try again") elif answer[0] == 'rm': # delete file or folder if len(answer) == 2: to_be_deleted = working_dir.find(answer[1]) if to_be_deleted: print(rm(to_be_deleted, working_dir, home)) else: print(f"Error. {answer[1]} - Element does not exist") else: print("Incorrect usage of: rm. Try again") elif answer[0] == 'ls': # print structure print(ls(working_dir, answer)) elif answer[0] == 'cat': # print file/folder info print(cat(working_dir, answer[1])) elif answer[0] == 'size': # print folder size try: size = working_dir.find(answer[1]).count_size_recursive() print(f'Size: {size} kB') except AttributeError: print("Incorrect usage of: size. Try again") elif answer[0] == 'wc': # count elements in folder try: print(wc(working_dir, answer)) except Exception: print("Incorrect usage of: wc. Try again") elif answer[0] == 'pwd': print(working_dir.name) elif answer[0] == 'cp': try: cp(working_dir, answer, home) except Exception: print("Incorrect usage of: cp. Try again") elif answer[0] == 'mv': try: mv(working_dir, answer, home) except Exception: print("Incorrect usage of: mv. Try again") elif answer[0] == 'help': print(help()) elif answer[0] == 'exit': # end program answer = False else: print("Command not found") except IndexError: print("No command was given. Try again.") main()
parser.add_argument("name", metavar="name", type=str, nargs="?", help="Name of the note, required for add and rm") # TODO document those parser.add_argument("-m", metavar="-m", type=str) parser.add_argument("-f", metavar="-f", type=str) args = parser.parse_args() if not is_valid_args(args): print("Invalid arguments, run --help for help") exit(error_code.INVALID_ARGUMENT) if args.command == "add": try: commands.add(**vars(args)) except ValueError as v: print(str(v)) exit(error_code.ADD_FAIL) elif args.command == "rm": try: commands.rm(**vars(args)) except FileNotFoundError: print("No such note") exit(error_code.NO_NOTE) elif args.command == "ls": print(commands.ls(**vars(args)), end="")
def main(): if not sys.argv: raise InvalidUsageException() argv = sys.argv[1:] # chop off script name as first arg try: command = argv[0] except IndexError: raise InvalidUsageException() if command == '--help': usage() elif argv[-1] == '--help': usage(command) elif command == 'init': try: repo_name = argv[1] except IndexError: raise InvalidUsageException('repo_name is required.') commands.init(repo_name) elif command == 'switch': try: repo_name = argv[1] except IndexError: raise InvalidUsageException('repo_name is required.') commands.switch(repo_name) elif command == 'add-remote': try: repo_uri = argv[1] except IndexError: raise InvalidUsageException('repo_uri is required.') try: remote_name = argv[2] except IndexError: remote_name = None commands.add_remote(repo_uri, remote_name=remote_name) elif command == 'drop-remote': try: remote_name = argv[1] except IndexError: raise InvalidUsageException('remote_name is required.') commands.drop_remote(remote_name) elif not active_repo: raise InvalidUsageException('no active repository, must init or switch to a repository.') elif command == 'ls': flags = _parse_flags(argv[1:]) commands.ls(tags=flags['tags'], neg_tags=flags['neg_tags']) elif command == 'add': try: description = argv[1] except IndexError: raise InvalidUsageException('Task description is required.') flags = _parse_flags(argv[2:]) if flags['neg_tags']: raise InvalidUsageException('Negative tags not valid when adding task.') for tag in flags['tags']: if tag in reserved_tags: raise InvalidUsageException('That tag is reserved. The following tags are automatically attached to tasks: %s.' % ', '.join(reserved_tags)) commands.add(description, priority=flags['priority'], tags=flags['tags']) elif command == 'mod': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) for tag in flags['tags'] + flags['neg_tags']: if tag in reserved_tags: raise InvalidUsageException('That tag is reserved. The following tags are automatically attached to tasks: %s.' % ', '.join(reserved_tags)) commands.mod(id, priority=flags['priority'], tags=flags['tags'], neg_tags=flags['neg_tags']) elif command == 'edit': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) if flags['priority'] or flags['tags'] or flags['neg_tags']: raise InvalidUsageException('Invalid options specified.') commands.edit(id) elif command == 'start': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) if flags['priority'] or flags['tags'] or flags['neg_tags']: raise InvalidUsageException('Invalid options specified.') commands.start(id) elif command == 'stop': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) if flags['priority'] or flags['tags'] or flags['neg_tags']: raise InvalidUsageException('Invalid options specified.') commands.stop(id) elif command == 'finish': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) if flags['priority'] or flags['tags'] or flags['neg_tags']: raise InvalidUsageException('Invalid options specified.') commands.finish(id) elif command == 'rm': try: id = argv[1] except IndexError: raise InvalidUsageException('id is required.') flags = _parse_flags(argv[2:]) if flags['priority'] or flags['tags'] or flags['neg_tags']: raise InvalidUsageException('Invalid options specified.') commands.rm(id) else: raise InvalidUsageException('Unrecognized command: %s.' % command)