Merge flags options

This commit is contained in:
Dimitris Zlatanidis 2023-01-10 20:09:33 +02:00
parent 0b2147d4b0
commit 19124d2bca
2 changed files with 77 additions and 86 deletions

View file

@ -61,7 +61,6 @@ class Argparse:
self.flag_full_reverse,
self.flag_search]
self.check_for_flags()
self.remove_flags()
def remove_flags(self):
@ -79,12 +78,79 @@ class Argparse:
"in the '/etc/slpkg/' folder.\n")
self.usage.help(1)
def check_for_flags(self):
def check_for_flags(self, command):
""" Check for correct flag. """
for opt in self.args:
commands = {
'--help': [],
'--version': [],
'update': [self.flag_yes],
'upgrade': [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_reinstall
],
'check-updates': [],
'configs': [],
'clean-logs': [self.flag_yes],
'clean-tmp': [],
'build': [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_search
],
'install': [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_reinstall,
self.flag_skip_installed,
self.flag_search
],
'download': [
self.flag_yes,
self.flag_search
],
'remove': [
self.flag_yes,
self.flag_resolve_off,
self.flag_search
],
'find': [self.flag_search],
'view': [self.flag_search],
'search': [self.flag_search],
'dependees': [
self.flag_full_reverse,
self.flag_search
],
'tracking': [self.flag_search]
}
commands['-h'] = commands['--help']
commands['-v'] = commands['--version']
commands['-b'] = commands['build']
commands['-i'] = commands['install']
commands['-d'] = commands['download']
commands['-r'] = commands['remove']
commands['-f'] = commands['find']
commands['-w'] = commands['view']
commands['-s'] = commands['search']
commands['-e'] = commands['dependees']
commands['-t'] = commands['tracking']
flags = commands[command]
for opt in self.flags:
if opt.startswith('--'):
if opt not in self.options and opt not in ['--help', '--version']:
raise SystemExit(f"\nError: flag '{opt}' does not exist.\n")
if opt not in flags and opt not in ['--help', '--version']:
self.usage.error_for_options(flags)
for arg in self.args:
if arg.startswith('--'):
if arg not in self.options and arg not in ['--help', '--version']:
self.usage.error_for_options(flags)
def choose_packages(self, packages, method):
""" Choose packages with dialog utility and --search flag. """
@ -152,11 +218,6 @@ class Argparse:
self.usage.help(1)
def update(self):
update_flags = [self.flag_yes]
if [f for f in self.flags if f not in update_flags]:
self.usage.error_for_options(update_flags)
if len(self.args) == 1:
update = UpdateRepository(self.flags)
update.repository()
@ -165,15 +226,6 @@ class Argparse:
def upgrade(self):
command = Argparse.upgrade.__name__
upgrade_flags = [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_reinstall
]
if [f for f in self.flags if f not in upgrade_flags]:
self.usage.error_for_options(upgrade_flags)
if len(self.args) == 1:
self.check.database()
@ -208,11 +260,6 @@ class Argparse:
self.usage.help(1)
def clean_logs(self):
clean_logs_flags = [self.flag_yes]
if [f for f in self.flags if f not in clean_logs_flags]:
self.usage.error_for_options(clean_logs_flags)
if len(self.args) == 1:
self.check.database()
@ -234,15 +281,6 @@ class Argparse:
def build(self):
command = Argparse.build.__name__
build_flags = [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_search
]
if [f for f in self.flags if f not in build_flags]:
self.usage.error_for_options(build_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -261,17 +299,6 @@ class Argparse:
def install(self):
command = Argparse.install.__name__
install_flags = [
self.flag_yes,
self.flag_jobs,
self.flag_resolve_off,
self.flag_reinstall,
self.flag_skip_installed,
self.flag_search
]
if [f for f in self.flags if f not in install_flags]:
self.usage.error_for_options(install_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -290,13 +317,6 @@ class Argparse:
def download(self):
command = Argparse.download.__name__
download_flags = [
self.flag_yes,
self.flag_search
]
if [f for f in self.flags if f not in download_flags]:
self.usage.error_for_options(download_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -313,14 +333,6 @@ class Argparse:
def remove(self):
command = Argparse.remove.__name__
remove_flags = [
self.flag_yes,
self.flag_resolve_off,
self.flag_search
]
if [f for f in self.flags if f not in remove_flags]:
self.usage.error_for_options(remove_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -338,10 +350,6 @@ class Argparse:
def find(self):
command = Argparse.find.__name__
find_flags = [self.flag_search]
if [f for f in self.flags if f not in find_flags]:
self.usage.error_for_options(find_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -358,10 +366,6 @@ class Argparse:
def view(self):
command = Argparse.view.__name__
view_flags = [self.flag_search]
if [f for f in self.flags if f not in view_flags]:
self.usage.error_for_options(view_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -379,10 +383,6 @@ class Argparse:
def search(self):
command = Argparse.search.__name__
search_flags = [self.flag_search]
if [f for f in self.flags if f not in search_flags]:
self.usage.error_for_options(search_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -399,13 +399,6 @@ class Argparse:
def dependees(self):
command = Argparse.dependees.__name__
dependees_flags = [
self.flag_full_reverse,
self.flag_search
]
if [f for f in self.flags if f not in dependees_flags]:
self.usage.error_for_options(dependees_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -423,10 +416,6 @@ class Argparse:
def tracking(self):
command = Argparse.tracking.__name__
tracking_flags = [self.flag_search]
if [f for f in self.flags if f not in tracking_flags]:
self.usage.error_for_options(tracking_flags)
if len(self.args) >= 2:
packages = list(set(self.args[1:]))
@ -481,6 +470,7 @@ def main():
}
try:
argparse.check_for_flags(args[0])
arguments[args[0]]()
except KeyError:
Usage().help(1)

View file

@ -71,9 +71,10 @@ class Usage:
print(args)
raise SystemExit(status)
@staticmethod
def error_for_options(flags):
def error_for_options(self, flags):
""" Error message for flags. """
print(f'Usage: {Configs.prog_name} [OPTIONS] [COMMAND] <packages>')
print(f'Usage: {Configs.prog_name} [{self.yellow}OPTIONS{self.endc}] '
f'[{self.cyan}COMMAND{self.endc}] <packages>')
print("Try 'slpkg --help' for help.\n")
raise SystemExit(f"Error: Got an unexpected extra option, please use: \n{', '.join(flags)}")
raise SystemExit(f"{self.red}Error:{self.endc} Got an unexpected extra option, "
f"please use: \n{self.yellow}{', '.join(flags)}{self.endc}")