Updated for invalid options

This commit is contained in:
Dimitris Zlatanidis 2023-03-16 19:31:48 +02:00
parent b715f9674b
commit 0e69f6e505

View file

@ -263,61 +263,25 @@ class Argparse(Configs):
self.split_options()
self.split_options_from_args()
self.invalid_options()
self.move_options()
self.invalid_options()
def invalid_options(self):
""" Checks for invalid options. """
commands: list = []
options: list = []
invalid: list = []
doubles: list = []
cache: list = []
error: int = 0
for arg in self.args:
if arg in self.options:
options.append(arg)
if arg[0] == '-' and arg in self.commands.keys():
commands.append(arg)
elif arg in self.commands.keys() and arg != 'help':
commands.append(arg)
if arg in self.commands.keys():
pass
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 add same options, exp: -P, --parallel.
opt_list = list(zip(self.options[::2], self.options[1::2]))
for i, opt1 in enumerate(opt_list):
# Reset cache by two items.
if (i % 2) == 0:
cache = []
for opt2 in options:
if opt2 in opt1:
cache.append(opt2)
if len(cache) == 2:
doubles = cache
if doubles:
print(f"{self.prog_name}: you added the same \'{', '.join(doubles)}\' options.")
error: int = 1
# Avoid to combine two or more commands.
if len(commands) > 1:
print(f"{self.prog_name}: you can't combine \'{', '.join(commands)}\' commands.")
error: int = 1
# Prints error for invalid options.
if invalid:
for opt in invalid:
print(f"{self.prog_name}: invalid option '{opt}'")
error: int = 1
if error:
self.usage.help_minimal()
def split_options(self) -> None: