|
12 | 12 | import argparse |
13 | 13 | import binascii |
14 | 14 |
|
| 15 | +from typing import Any, Sequence, cast |
| 16 | + |
15 | 17 | from sender import broadcast_packet |
16 | 18 | from packet import pkg_message, pkg_shutdown, pkg_rename, pkg_website, pkg_execute |
17 | 19 |
|
18 | 20 |
|
| 21 | +class ModeOptionalAction(argparse.Action): |
| 22 | + """ |
| 23 | + Custom action for handling optional arguments in argparse. |
| 24 | + This action allows the user to specify a mode (e.g., --max or --min) for the program execution. |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, option_strings: Sequence[str], dest: str, modes: Sequence[str], **kwargs: Any): |
| 28 | + self.modes = list(modes) |
| 29 | + |
| 30 | + if any("-" in mode for mode in self.modes): |
| 31 | + raise ValueError("Modes cannot contain '-' characters. Please use a different character.") |
| 32 | + |
| 33 | + _option_strings = [] |
| 34 | + for option in option_strings: |
| 35 | + _option_strings.append(option) |
| 36 | + |
| 37 | + for mode in self.modes: |
| 38 | + if option.startswith("--"): |
| 39 | + if option.startswith(f"--{mode}-"): |
| 40 | + raise ValueError( |
| 41 | + f"Option '{option}' cannot start with '--{mode}-'. " |
| 42 | + "Please use a different prefix for modes." |
| 43 | + ) |
| 44 | + _option_strings.append(f"--{mode}-{option[2:]}") |
| 45 | + |
| 46 | + return super().__init__(_option_strings, dest, **kwargs) |
| 47 | + |
| 48 | + def __call__( |
| 49 | + self, |
| 50 | + parser: argparse.ArgumentParser, |
| 51 | + namespace: argparse.Namespace, |
| 52 | + values: Any, |
| 53 | + option_string: str | None = None, |
| 54 | + ): |
| 55 | + if option_string in self.option_strings: |
| 56 | + option_string = cast(str, option_string) |
| 57 | + if option_string.startswith("--"): |
| 58 | + mode = option_string.split("-")[2] |
| 59 | + if mode not in self.modes: |
| 60 | + mode = None |
| 61 | + else: |
| 62 | + mode = None |
| 63 | + setattr(namespace, self.dest, (mode, values)) |
| 64 | + |
| 65 | + def format_usage(self) -> str: |
| 66 | + return " | ".join(self.option_strings) |
| 67 | + |
| 68 | + |
19 | 69 | if __name__ == "__main__": |
20 | 70 | parser = argparse.ArgumentParser( |
21 | 71 | description="Jiyu Attack Script", |
|
0 commit comments