From b40db6f6fdcd113ec5f199986b32e605f0a81861 Mon Sep 17 00:00:00 2001 From: Dimitris Zlatanidis Date: Thu, 16 Mar 2023 12:47:35 +0200 Subject: [PATCH] Moved to new method --- slpkg/main.py | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/slpkg/main.py b/slpkg/main.py index 6a948235..7c7fbdcc 100644 --- a/slpkg/main.py +++ b/slpkg/main.py @@ -263,8 +263,36 @@ class Argparse(Configs): self.split_options() self.split_options_from_args() + self.invalid_options() self.move_options() + def invalid_options(self): + """ Checks for invalid options. """ + commands: list = [] + invalid: list = [] + + for arg in self.args: + if arg[0] == '-' and arg in self.commands.keys(): + commands.append(arg) + elif arg in self.commands.keys(): + commands.append(arg) + + elif arg[0] == '-' and arg not in self.options: + invalid.append(arg) + elif arg[0] == '--' and arg not in self.options: + invalid.append(arg) + + # # Avoid to combine two or more commands. + if len(commands) > 1: + print(f"slpkg: You can't combine {', '.join(commands)} commands.") + self.usage.help_minimal() + + # Prints error for invalid options. + if invalid: + for opt in invalid: + print(f"slpkg: invalid option '{opt}'") + self.usage.help_minimal() + def split_options(self) -> None: """ Split options and commands, like: -iyjR @@ -273,35 +301,17 @@ class Argparse(Configs): Puts the command first and options after. Result: ['-i', '-y', '-j', '-R'] """ - invalid_options: list = [] - commands: list = [] for args in self.args: if args[0] == '-' and args[:2] != '--' and len(args) >= 3 and '=' not in args: self.args.remove(args) for opt in list(map(lambda item: f'-{item}', [arg for arg in list(args[1:])])): - if opt in self.commands.keys(): - commands.append(opt) - if opt in self.commands.keys(): self.args.insert(0, opt) continue - elif opt not in self.options: - invalid_options.append(opt) self.args.append(opt) - # Avoid to combine two or more commands. - if len(commands) > 1: - print(f"slpkg: You can't combine {', '.join(commands)} commands.") - self.usage.help_minimal() - - # Prints error for invalid options. - if invalid_options: - for opt in invalid_options: - print(f"slpkg: invalid option '{opt}'") - self.usage.help_minimal() - def split_options_from_args(self) -> None: """ Split options from arguments. @@ -328,6 +338,9 @@ class Argparse(Configs): def move_options(self) -> None: """ Move options to the flags and removes from the arguments. """ + # Removes doubles and keeps the order. + self.args = list(dict.fromkeys(self.args)) + for opt in self.options: if opt in self.args: self.args.remove(opt)