sdispater / clikit

CliKit is a group of utilities to build beautiful and testable command line interfaces.
MIT License
72 stars 17 forks source link

avoid displaying a stacktrace when user provide a wrong option #38

Closed jouve closed 3 years ago

jouve commented 3 years ago

fix https://github.com/python-poetry/poetry/issues/3273 https://github.com/python-poetry/poetry/issues/2627 https://github.com/python-poetry/poetry/issues/2854

display:

poetry add --hrlp
The "--hrlp" option does not exist.

instead of:


  Stack trace:

  11  ~/xxx/lib/python3.9/site-packages/clikit/console_application.py:123 in run
      io = io_factory(

  10  ~/xxx/lib/python3.9/site-packages/poetry/console/config/application_config.py:223 in create_io
      resolved_command = application.resolve_command(args)

   9  ~/xxx/lib/python3.9/site-packages/clikit/console_application.py:110 in resolve_command
      return self._config.command_resolver.resolve(args, self)

   8  ~/xxx/lib/python3.9/site-packages/clikit/resolver/default_resolver.py:34 in resolve
      return self.create_resolved_command(result)

   7  ~/xxx/lib/python3.9/site-packages/clikit/resolver/default_resolver.py:166 in create_resolved_command
      if not result.is_parsable():

   6  ~/xxx/lib/python3.9/site-packages/clikit/resolver/resolve_result.py:43 in is_parsable
      self._parse()

   5  ~/xxx/lib/python3.9/site-packages/clikit/resolver/resolve_result.py:49 in _parse
      self._parsed_args = self._command.parse(self._raw_args)

   4  ~/xxx/lib/python3.9/site-packages/clikit/api/command/command.py:113 in parse
      return self._config.args_parser.parse(args, self._args_format, lenient)

   3  ~/xxx/lib/python3.9/site-packages/clikit/args/default_args_parser.py:53 in parse
      self._parse(args, _fmt, lenient)

   2  ~/xxx/lib/python3.9/site-packages/clikit/args/default_args_parser.py:101 in _parse
      self._parse_long_option(token, tokens, fmt, lenient)

   1  ~/xxx/lib/python3.9/site-packages/clikit/args/default_args_parser.py:247 in _parse_long_option
      self._add_long_option(name, None, tokens, fmt, lenient)

  NoSuchOptionException

  The "--hrlp" option does not exist.

  at ~/xxx/lib/python3.9/site-packages/clikit/args/default_args_parser.py:300 in _add_long_option
      296│     def _add_long_option(
      297│         self, name, value, tokens, fmt, lenient
      298│     ):  # type: (str, Optional[str], List[str], ArgsFormat, bool) -> None
      299│         if not fmt.has_option(name):
    → 300│             raise NoSuchOptionException(name)
      301│
      302│         option = fmt.get_option(name)
      303│
      304│         if value is False:
jouve commented 3 years ago

@sdispater I'm not sure about the Tests failure


  KeyError

  'VIRTUAL_ENV'

  at /opt/hostedtoolcache/Python/3.6.12/x64/lib/python3.6/os.py:669 in __getitem__
       665│         try:
       666│             value = self._data[self.encodekey(key)]
       667│         except KeyError:
       668│             # raise KeyError with the original key value
    →  669│             raise KeyError(key) from None
       670│         return self.decodevalue(value)
       671│ 
       672│     def __setitem__(self, key, value):
       673│         key = self.encodekey(key)
sdispater commented 3 years ago

Thanks!