Uberspace / paternoster

Paternoster allows you to run Ansible playbooks like ordinary Python or Bash scripts.
Other
122 stars 4 forks source link

no_echo prompt echoes value in error message #26

Open luto opened 6 years ago

luto commented 6 years ago
- 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 commented 6 years ago

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 __getvalue 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))
brutus commented 6 years ago

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 _noecho options:

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