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

no_echo prompt echoes value in error message #26

Open
luto opened this issue Feb 16, 2018 · 2 comments
Open

no_echo prompt echoes value in error message #26

luto opened this issue Feb 16, 2018 · 2 comments

Comments

@luto
Copy link
Member

luto commented Feb 16, 2018

- hosts: paternoster
  vars:
    parameters:
      - name: domain
        short: d
        type: paternoster.types.domain
        prompt: yes
        prompt_options:
          confirm: yes
          no_echo: yes
[root@centos-s-2vcpu-4gb-fra1-01 ~]# ./add-domain 
Domain: (enter "qwe")
Please confirm: (enter "qwe")
usage: add-domain [-h] [-d DOMAIN] [-v]
add-domain: error: argument -d/--domain: invalid domain value: 'qwe'
@brutus
Copy link
Contributor

brutus commented Feb 17, 2018

We are deep in Argparser territory here. The strings are printed by the exit
method form the ArgumentParser class. The logic that raises this is in the
internal _get_value methode:

def _get_value(self, action, arg_string):
    type_func = self._registry_get('type', action.type, action.type)
    if not _callable(type_func):
        msg = _('%r is not callable')
        raise ArgumentError(action, msg % type_func)

    # convert the value to the appropriate type
    try:
        result = type_func(arg_string)

    # ArgumentTypeErrors indicate errors
    except ArgumentTypeError:
        name = getattr(action.type, '__name__', repr(action.type))
        msg = str(_sys.exc_info()[1])
        raise ArgumentError(action, msg)

    # TypeErrors or ValueErrors also indicate errors
    except (TypeError, ValueError):
        name = getattr(action.type, '__name__', repr(action.type))
        msg = _('invalid %s value: %r')
        raise ArgumentError(action, msg % (name, arg_string))

@luto luto removed the lowhanging label Feb 18, 2018
@brutus
Copy link
Contributor

brutus commented Mar 28, 2018

One workaround could be to subclass the ArgumentParser class and override its error method, to raise errors as exception instead of printing and exiting, so that we could handle the output ourself and restrict it for no_echo options:

https://github.com/python/cpython/blob/a2665075cc2fb85129afdfd7a7f04dd70a38e582/Lib/argparse.py#L2364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants