Skip to content

Commit

Permalink
cli: add method to get help for any command directly from parser
Browse files Browse the repository at this point in the history
And the parser is now ProArgumentParser

Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo committed Jul 13, 2024
1 parent f4a79b3 commit e74418e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 32 deletions.
14 changes: 12 additions & 2 deletions uaclient/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
]


class UAArgumentParser(argparse.ArgumentParser):
class ProArgumentParser(argparse.ArgumentParser):
def error(self, message):
self.print_usage(sys.stderr)
# In some cases (e.g. `pro --wrong-flag`) argparse errors out asking
Expand All @@ -72,9 +72,19 @@ def error(self, message):
message = messages.CLI_TRY_HELP
self.exit(2, message + "\n")

def print_help_for_command(self, command: str):
args_list = command.split()
args_list.append("--help")
try:
self.parse_args(args_list)
# We want help for any specific command,
# but without exiting right after
except SystemExit:
pass


def get_parser(cfg: UAConfig):
parser = UAArgumentParser(
parser = ProArgumentParser(
prog=NAME,
formatter_class=argparse.RawDescriptionHelpFormatter,
usage=USAGE_TMPL.format(name=NAME, command="<command>"),
Expand Down
34 changes: 6 additions & 28 deletions uaclient/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def action_config(args, *, cfg, **kwargs):
# Avoiding a circular import
from uaclient.cli import get_parser

# Pretty sure there will be some method to do this soon
# so we don't repeat it all over the place
get_parser(cfg).parse_args(["config", "--help"])
get_parser(cfg).print_help_for_command("config")
return 0


Expand Down Expand Up @@ -74,29 +72,17 @@ def action_config_set(args, *, cfg, **kwargs):
try:
set_key, set_value = args.key_value_pair.split("=")
except ValueError:
try:
parser.parse_args(["config", "set", "--help"])
# Excepting SystemExit so we can raise our own exception
except SystemExit:
pass
parser.print_help_for_command("config set")
raise exceptions.GenericInvalidFormat(
expected="<key>=<value>", actual=args.key_value_pair
)
if set_key not in config.UA_CONFIGURABLE_KEYS:
try:
parser.parse_args(["config", "set", "--help"])
# Excepting SystemExit so we can raise our own exception
except SystemExit:
pass
parser.print_help_for_command("config set")
raise exceptions.InvalidArgChoice(
arg="<key>", choices=", ".join(config.UA_CONFIGURABLE_KEYS)
)
if not set_value.strip():
try:
parser.parse_args(["config", "set", "--help"])
# Excepting SystemExit so we can raise our own exception
except SystemExit:
pass
parser.print_help_for_command("config set")
raise exceptions.EmptyConfigValue(arg=set_key)
if set_key in ("http_proxy", "https_proxy"):
protocol_type = set_key.split("_")[0]
Expand Down Expand Up @@ -182,11 +168,7 @@ def action_config_set(args, *, cfg, **kwargs):
if set_value < 0:
raise ValueError("Invalid interval for {}".format(set_key))
except ValueError:
try:
parser.parse_args(["config", "set", "--help"])
# Excepting SystemExit so we can raise our own exception
except SystemExit:
pass
parser.print_help_for_command("config set")
# More readable in the CLI, without breaking the line in the logs
print("")
raise exceptions.InvalidPosIntConfigValue(
Expand All @@ -212,11 +194,7 @@ def action_config_unset(args, *, cfg, **kwargs):

if args.key not in config.UA_CONFIGURABLE_KEYS:
parser = get_parser(cfg=cfg)
try:
parser.parse_args(["config", "unset", "--help"])
# Excepting SystemExit so we can raise our own exception
except SystemExit:
pass
parser.print_help_for_command("config unset")
raise exceptions.InvalidArgChoice(
arg="<key>", choices=", ".join(config.UA_CONFIGURABLE_KEYS)
)
Expand Down
2 changes: 1 addition & 1 deletion uaclient/cli/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def action_help(args, *, cfg, **kwargs):
service = args.service

if not service:
# inserting it here to avoid circular imports
# Avoiding a circular import
from uaclient.cli import get_parser

get_parser(cfg=cfg).print_help()
Expand Down
2 changes: 1 addition & 1 deletion uaclient/cli/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def action_system(args, *, cfg, **kwargs):
# Avoiding a circular import
from uaclient.cli import get_parser

get_parser(cfg).parse_args(["system", "--help"])
get_parser(cfg).print_help_for_command("system")


reboot_required_subcommand = ProCommand(
Expand Down

0 comments on commit e74418e

Please sign in to comment.