1
0
mirror of https://github.com/facebook/proxygen.git synced 2025-08-10 05:22:59 +03:00
Files
proxygen/build/fbcode_builder/getdeps/subcmd.py
Pyre Bot Jr e77a9fe43a Add annotations to opensource/fbcode_builder
Reviewed By: shannonzhu

Differential Revision: D34224272

fbshipit-source-id: 52e19886ab3d4fb015a557244660dd4357a35c17
2022-02-14 16:22:09 -08:00

57 lines
1.4 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
class SubCmd(object):
NAME = None
HELP = None
def run(self, args) -> int:
"""perform the command"""
return 0
def setup_parser(self, parser) -> None:
# Subclasses should override setup_parser() if they have any
# command line options or arguments.
pass
CmdTable = []
def add_subcommands(parser, common_args, cmd_table=CmdTable) -> None:
"""Register parsers for the defined commands with the provided parser"""
for cls in cmd_table:
command = cls()
command_parser = parser.add_parser(
command.NAME, help=command.HELP, parents=[common_args]
)
command.setup_parser(command_parser)
command_parser.set_defaults(func=command.run)
def cmd(name, help=None, cmd_table=CmdTable):
"""
@cmd() is a decorator that can be used to help define Subcmd instances
Example usage:
@subcmd('list', 'Show the result list')
class ListCmd(Subcmd):
def run(self, args):
# Perform the command actions here...
pass
"""
def wrapper(cls):
class SubclassedCmd(cls):
NAME = name
HELP = help
cmd_table.append(SubclassedCmd)
return SubclassedCmd
return wrapper