Skip to content

Commit

Permalink
Moved validate_required_arg() to common _options.py module
Browse files Browse the repository at this point in the history
Details:

* The 'pywbemcli instance' command group has a --help-instancename option that
  shows a help message for instance names. The instance commands that have
  this option all define their positional arguments at the Click level with
  required=False, even when they are actually required. If they are required,
  the presence of the argument is validated using the validate_required_arg()
  function that is invoked after the --help-... option has been processed.
  This allows using the --help-... option without having to specify required
  positional arguments.

  This change moves the validate_required_arg() function from the
  pywbemcli/_cmd_instance.py module to the common _options.py module so it
  can be used also by the future pywbemlistener command.

Signed-off-by: Andreas Maier <[email protected]>
  • Loading branch information
andy-maier committed May 15, 2021
1 parent 569aeef commit a1f0e2f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
16 changes: 16 additions & 0 deletions pywbemtools/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ def _add_options(func):
return func

return _add_options


def validate_required_arg(value, metavar):
"""
Validate that a required CLI argument is present, and raise a usage error
otherwise.
This function is used for required arguments in cases where the command
has a --help-... option, so that the option can be used without specifying
the required arguments. Just using is_eager on the option is not sufficient
for that.
"""
if not value:
raise click.UsageError(
"Missing argument '{}'.".format(metavar),
click.get_current_context())
13 changes: 1 addition & 12 deletions pywbemtools/pywbemcli/_cmd_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from ._cmd_class import get_namespaces, enumerate_classes_filtered
from .._click_extensions import PywbemtoolsGroup, PywbemtoolsCommand, \
CMD_OPTS_TXT, GENERAL_OPTS_TXT, SUBCMD_HELP_TXT
from .._options import add_options, help_option
from .._options import add_options, help_option, validate_required_arg
from .._output_formatting import validate_output_format, format_table, \
warning_msg

Expand Down Expand Up @@ -675,17 +675,6 @@ def instance_shrub(context, instancename, **options):
####################################################################


def validate_required_arg(value, metavar):
"""
Validate that a required CLI argument is present, and raise a usage error
otherwise.
"""
if not value:
raise click.UsageError(
"Missing argument '{}'.".format(metavar),
click.get_current_context())


def show_help_instancename():
"""
Show the help message on how to specify an instance using INSTANCENAME
Expand Down

0 comments on commit a1f0e2f

Please sign in to comment.