Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update command.py #298

Closed
wants to merge 1 commit into from
Closed

Conversation

RobThePCGuy
Copy link

  1. Error on Windows running pokete.py:
Traceback (most recent call last):
  File "C:\Users\Rob\Downloads\pokete\pokete.py", line 63, in <module>
    from util.command import RootCommand, Flag
  File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in <module>
    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:

    @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
        )
  1. Error after applying the previous fix:
Traceback (most recent call last):
  File "C:\Users\Rob\Downloads\pokete\pokete.py", line 63, in <module>
    from util.command import RootCommand, Flag
  File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in <module>
    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:

    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 ""} <flags>
{options_section}
{flags_section}
{additional_info_section}Copyright (c) lxgr-linux <[email protected]> 2024"""
        )

Fixed.

1. Error on Windows running pokete.py:

```
Traceback (most recent call last):
  File "C:\Users\Rob\Downloads\pokete\pokete.py", line 63, in <module>
    from util.command import RootCommand, Flag
  File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in <module>
    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 <module>
    from util.command import RootCommand, Flag
  File "C:\Users\Rob\Downloads\pokete\util\command\__init__.py", line 1, in <module>
    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 ""} <flags>
{options_section}
{flags_section}
{additional_info_section}Copyright (c) lxgr-linux <[email protected]> 2024"""
        )
```

Fixed.
@lxgr-linux
Copy link
Owner

Hey thanks for your contribution.

The underlying issue here is that ur on a Python Version below 3.12.
From 3.12 onwards strings in f-strings are supported.
I won't accept this PR since in the multiplayer branch I'm heavily relying on newer python features so pulling down the minimum supported Python Version won't make sense here.

Still I really appreciate your contribution.

@lxgr-linux lxgr-linux closed this Jan 23, 2025
@RobThePCGuy RobThePCGuy deleted the patch-1 branch January 27, 2025 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants