Skip to content

Commit

Permalink
Add typing to SortingHelpFormatter in args.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwartzentruber committed May 29, 2024
1 parent d446c30 commit 63096dd
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions grizzly/args.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from argparse import ArgumentParser, HelpFormatter, Namespace
from argparse import (
Action,
ArgumentParser,
HelpFormatter,
Namespace,
_MutuallyExclusiveGroup,
)
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING
from os import getenv
from os.path import exists
from pathlib import Path
from platform import system
from typing import List, Optional
from typing import Iterable, List, Optional

from .common.fuzzmanager import FM_CONFIG, ProgramConfiguration
from FTB.ProgramConfiguration import ProgramConfiguration

from .common.fuzzmanager import FM_CONFIG
from .common.plugins import scan as scan_plugins
from .common.plugins import scan_target_assets
from .common.utils import DEFAULT_TIME_LIMIT, TIMEOUT_DELAY, __version__
Expand All @@ -18,17 +26,23 @@
# ref: https://stackoverflow.com/questions/12268602/sort-argparse-help-alphabetically
class SortingHelpFormatter(HelpFormatter):
@staticmethod
def __sort_key(action):
def __sort_key(action: Action) -> List[str]:
for opt in action.option_strings:
if opt.startswith("--"):
return [opt]
return action.option_strings

def add_usage(self, usage, actions, groups, prefix=None):
return list(action.option_strings)

def add_usage(
self,
usage: Optional[str],
actions: Iterable[Action],
groups: Iterable[_MutuallyExclusiveGroup],
prefix: Optional[str] = None,
) -> None:
actions = sorted(actions, key=self.__sort_key)
super().add_usage(usage, actions, groups, prefix)

def add_arguments(self, actions):
def add_arguments(self, actions: Iterable[Action]) -> None:
actions = sorted(actions, key=self.__sort_key)
super().add_arguments(actions)

Expand Down

0 comments on commit 63096dd

Please sign in to comment.