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

refactor(framework) Order CLI args alphabetically #4923

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/py/flwr/client/clientapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from flwr.client.client_app import ClientApp, LoadClientAppError
from flwr.common import Context, Message
from flwr.common.args import add_args_flwr_app_common
from flwr.common.args_utils import SortingHelpFormatter
from flwr.common.config import get_flwr_dir
from flwr.common.constant import CLIENTAPPIO_API_DEFAULT_CLIENT_ADDRESS, ErrorCode
from flwr.common.exit import ExitCode, flwr_exit
Expand Down Expand Up @@ -235,6 +236,7 @@ def _parse_args_run_flwr_clientapp() -> argparse.ArgumentParser:
"""Parse flwr-clientapp command line arguments."""
parser = argparse.ArgumentParser(
description="Run a Flower ClientApp",
formatter_class=SortingHelpFormatter,
)
parser.add_argument(
"--clientappio-api-address",
Expand Down
2 changes: 2 additions & 0 deletions src/py/flwr/client/supernode/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from flwr.common import EventType, event
from flwr.common.args import try_obtain_root_certificates
from flwr.common.args_utils import SortingHelpFormatter
from flwr.common.config import parse_config_args
from flwr.common.constant import (
CLIENTAPPIO_API_DEFAULT_SERVER_ADDRESS,
Expand Down Expand Up @@ -112,6 +113,7 @@ def _parse_args_run_supernode() -> argparse.ArgumentParser:
"""Parse flower-supernode command line arguments."""
parser = argparse.ArgumentParser(
description="Start a Flower SuperNode",
formatter_class=SortingHelpFormatter,
)
_parse_args_common(parser)
parser.add_argument(
Expand Down
42 changes: 42 additions & 0 deletions src/py/flwr/common/args_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2025 Flower Labs GmbH. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Flower argument utilities."""


import argparse
from argparse import Action, _MutuallyExclusiveGroup
from typing import Iterable, List, Optional


class SortingHelpFormatter(argparse.HelpFormatter):
"""Sort the arguments alphabetically in the help text."""

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

# def add_usage(self, usage, actions, groups, prefix=None) -> None:
# Sort the usage actions alphabetically
sorted_actions: List[Action] = sorted(actions, key=lambda action: action.dest)
super().add_usage(usage, sorted_actions, groups, prefix)

def add_arguments(self, actions: Iterable[Action]) -> None:
# Sort the argument actions alphabetically
sorted_actions: List[Action] = sorted(actions, key=lambda action: action.dest)
super().add_arguments(sorted_actions)
3 changes: 3 additions & 0 deletions src/py/flwr/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from flwr.common import GRPC_MAX_MESSAGE_LENGTH, EventType, event
from flwr.common.address import parse_address
from flwr.common.args import try_obtain_server_certificates
from flwr.common.args_utils import SortingHelpFormatter
from flwr.common.auth_plugin import ExecAuthPlugin
from flwr.common.config import get_flwr_dir, parse_config_args
from flwr.common.constant import (
Expand Down Expand Up @@ -691,8 +692,10 @@ def _run_fleet_api_rest(

def _parse_args_run_superlink() -> argparse.ArgumentParser:
"""Parse command line arguments for both ServerAppIo API and Fleet API."""

parser = argparse.ArgumentParser(
description="Start a Flower SuperLink",
formatter_class=SortingHelpFormatter,
)

_add_args_common(parser=parser)
Expand Down
2 changes: 2 additions & 0 deletions src/py/flwr/server/serverapp/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from flwr.cli.install import install_from_fab
from flwr.cli.utils import get_sha256_hash
from flwr.common.args import add_args_flwr_app_common
from flwr.common.args_utils import SortingHelpFormatter
from flwr.common.config import (
get_flwr_dir,
get_fused_config_from_dir,
Expand Down Expand Up @@ -240,6 +241,7 @@ def _parse_args_run_flwr_serverapp() -> argparse.ArgumentParser:
"""Parse flwr-serverapp command line arguments."""
parser = argparse.ArgumentParser(
description="Run a Flower ServerApp",
formatter_class=SortingHelpFormatter,
)
parser.add_argument(
"--serverappio-api-address",
Expand Down
Loading