From 419035363c09bbaec8cd35750c69efecc05e9851 Mon Sep 17 00:00:00 2001 From: Rob Adams Jr Date: Tue, 21 Jan 2025 23:51:34 -0600 Subject: [PATCH] Update command.py 1. Error on Windows running pokete.py: ``` Traceback (most recent call last): File "C:\Users\Rob\Downloads\pokete\pokete.py", line 63, in from util.command import RootCommand, Flag File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in from .command import Command, RootCommand, Flag File "C:\Users\Rob\Downloads\pokete\util\command\command.py", line 37 f"\t{line[0]}{" " * (line_spaces - len(line[0]))}{line[1]}" for line ^^^ SyntaxError: f-string: expecting '}' ``` Which can be fixed by the following: ```python @staticmethod def __line_setter(lines: list[tuple[str, str]], line_spaces: int): return "\n".join( f"\t{line[0]}{' ' * (line_spaces - len(line[0]))}{line[1]}" for line in lines ) ``` 2. Error after applying the previous fix: ``` Traceback (most recent call last): File "C:\Users\Rob\Downloads\pokete\pokete.py", line 63, in from util.command import RootCommand, Flag File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in from .command import Command, RootCommand, Flag File "C:\Users\Rob\Downloads\pokete\util\command\command.py", line 56 Options: ^^^^^^^ SyntaxError: f-string: expecting '}' ``` Which can be fixed by the following: ```python def __print_help(self, ex: str): option_lines: list[tuple[str, str]] = [(command.name, command.desc) for command in self.commands] flag_lines: list[tuple[str, str]] = [("|".join(flag.aliases), flag.desc) for flag in self.flags] line_spaces = max((len(i[0]) for i in option_lines + flag_lines), default=0) + 8 options_section = ( f"Options:\n{self.__line_setter(option_lines, line_spaces)}" if self.commands else "" ) flags_section = ( f"Flags:\n{self.__line_setter(flag_lines, line_spaces)}" if self.flags else "" ) additional_info_section = f"\n{self.additional_info}\n" if self.additional_info else "" print( f"""{self.name} -- {self.desc} Usage: {ex}{f" {self.usage}" if self.usage else ""} {options_section} {flags_section} {additional_info_section}Copyright (c) lxgr-linux 2024""" ) ``` Fixed. --- util/command/command.py | 43 +++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/util/command/command.py b/util/command/command.py index 825f576a..1a193e53 100644 --- a/util/command/command.py +++ b/util/command/command.py @@ -34,34 +34,31 @@ def __init__( @staticmethod def __line_setter(lines: list[tuple[str, str]], line_spaces: int): return "\n".join( - f"\t{line[0]}{" " * (line_spaces - len(line[0]))}{line[1]}" for line - in lines + f"\t{line[0]}{' ' * (line_spaces - len(line[0]))}{line[1]}" + for line in lines ) def __print_help(self, ex: str): - option_lines: list[tuple[str, str]] = [(command.name, command.desc) for - command in self.commands] - flag_lines: list[tuple[str, str]] = [ - ("|".join(flag.aliases), flag.desc) for flag in self.flags] - - line_spaces = sorted([ - len(i[0]) for i in option_lines + flag_lines - ])[-1] + 8 - - print(f"""{self.name} -- {self.desc} - + option_lines: list[tuple[str, str]] = [(command.name, command.desc) for command in self.commands] + flag_lines: list[tuple[str, str]] = [("|".join(flag.aliases), flag.desc) for flag in self.flags] + line_spaces = max((len(i[0]) for i in option_lines + flag_lines), default=0) + 8 + options_section = ( + f"Options:\n{self.__line_setter(option_lines, line_spaces)}" + if self.commands else "" + ) + flags_section = ( + f"Flags:\n{self.__line_setter(flag_lines, line_spaces)}" + if self.flags else "" + ) + additional_info_section = f"\n{self.additional_info}\n" if self.additional_info else "" + print( + f"""{self.name} -- {self.desc} Usage: {ex}{f" {self.usage}" if self.usage else ""} -{f""" -Options: -{self.__line_setter(option_lines, line_spaces)} -""" if self.commands else ""} -{f""" -Flags: -{self.__line_setter(flag_lines, line_spaces)} -""" if self.flags else ""} -{f"\n{self.additional_info}\n" if self.additional_info else ""} -Copyright (c) lxgr-linux 2024""") +{options_section} +{flags_section} +{additional_info_section}Copyright (c) lxgr-linux 2024""" + ) def run(self, ex: str, options: list[str], flags: dict[str, list[str]]):