From 346c2520f4fe54590348b33a0c5baea58a6a226f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Prpi=C4=8D?= Date: Thu, 3 Oct 2024 10:44:56 +0200 Subject: [PATCH] Do not require authentication credentials for subcommands that don't need them Subcommands such as `ping` or `show` hit unauthenticated endpoints, so they do not need to enforce users to provide them via the global -u/-a/-o options. --- cvelib/cli.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cvelib/cli.py b/cvelib/cli.py index c5479c0..118b91c 100644 --- a/cvelib/cli.py +++ b/cvelib/cli.py @@ -220,14 +220,19 @@ def init_cve_api(self) -> CveApi: ) -class SkipRequiredOnHelp(click.Group): +class SkipRequiredIf(click.Group): def parse_args(self, ctx: click.Context, args: list) -> list: - """If any help options are used, mark global required options as not required. + """Mark global required options as not required in certain cases - That way, we can skip directly to showing the help without requiring users to specify - values that don't end up getting used anyway. + If any help options are used, we can skip directly to showing the help without requiring + users to specify authentication values. + + If certain subcommands are used that don't require authentication (e.g. `ping`), do not + require authentication values. """ - if any(arg in ctx.help_option_names for arg in args): + if any(arg in ctx.help_option_names for arg in args) or any( + arg in ("ping", "show") for arg in args + ): # Iterate over all options and flip them to not required and not to prompt for input. for param in self.params: if isinstance(param, click.Option): @@ -235,12 +240,18 @@ def parse_args(self, ctx: click.Context, args: list) -> list: # Type ignored due to `"Option" has no attribute "prompt_required"` error: # https://github.com/pallets/click/blob/d0af32d8/src/click/core.py#L2455 param.prompt_required = False # type: ignore - return super(SkipRequiredOnHelp, self).parse_args(ctx, args) + return super(SkipRequiredIf, self).parse_args(ctx, args) -@click.group(context_settings=CONTEXT_SETTINGS, cls=SkipRequiredOnHelp) +@click.group(context_settings=CONTEXT_SETTINGS, cls=SkipRequiredIf) @click.option( - "-u", "--username", envvar="CVE_USER", required=True, help="Your username (env var: CVE_USER)" + "-u", + "--username", + envvar="CVE_USER", + required=True, + help="Your username (env var: CVE_USER)", + prompt="Username", + hide_input=True, ) @click.option( "-o", @@ -248,6 +259,8 @@ def parse_args(self, ctx: click.Context, args: list) -> list: envvar="CVE_ORG", required=True, help="Your CNA organization short name (env var: CVE_ORG)", + prompt="Organization", + hide_input=True, ) @click.option( "-a",