Moved to new method

This commit is contained in:
Dimitris Zlatanidis 2023-03-16 12:47:35 +02:00
parent f3dd31b398
commit b40db6f6fd

View file

@ -263,8 +263,36 @@ class Argparse(Configs):
self.split_options() self.split_options()
self.split_options_from_args() self.split_options_from_args()
self.invalid_options()
self.move_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: def split_options(self) -> None:
""" Split options and commands, like: -iyjR """ Split options and commands, like: -iyjR
@ -273,35 +301,17 @@ class Argparse(Configs):
Puts the command first and options after. Puts the command first and options after.
Result: ['-i', '-y', '-j', '-R'] Result: ['-i', '-y', '-j', '-R']
""" """
invalid_options: list = []
commands: list = []
for args in self.args: for args in self.args:
if args[0] == '-' and args[:2] != '--' and len(args) >= 3 and '=' not in args: if args[0] == '-' and args[:2] != '--' and len(args) >= 3 and '=' not in args:
self.args.remove(args) self.args.remove(args)
for opt in list(map(lambda item: f'-{item}', [arg for arg in list(args[1:])])): 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(): if opt in self.commands.keys():
self.args.insert(0, opt) self.args.insert(0, opt)
continue continue
elif opt not in self.options:
invalid_options.append(opt)
self.args.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: def split_options_from_args(self) -> None:
""" Split options from arguments. """ Split options from arguments.
@ -328,6 +338,9 @@ class Argparse(Configs):
def move_options(self) -> None: def move_options(self) -> None:
""" Move options to the flags and removes from the arguments. """ """ 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: for opt in self.options:
if opt in self.args: if opt in self.args:
self.args.remove(opt) self.args.remove(opt)