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

Misleading error message for invalid choice arguments #236

Closed
fralik opened this issue Mar 2, 2021 · 0 comments · Fixed by #244
Closed

Misleading error message for invalid choice arguments #236

fralik opened this issue Mar 2, 2021 · 0 comments · Fixed by #244

Comments

@fralik
Copy link

fralik commented Mar 2, 2021

When an argument has a list of available choices and user provides an invalid entry, I suppose to see an error message related to that. Something that is aligned with argparser documentation:

game.py: error: argument move: invalid choice: 'fire' (choose from 'rock',
'paper', 'scissors')

However, Knack gives an error message like this mycli abc str: 'fire' is not in the 'mycli abc str' command group. See 'mycli abc str --help'.

A minimum example (mycli.py), click to see the code
import sys
from collections import OrderedDict

from knack import CLI, ArgumentsContext, CLICommandsLoader
from knack.commands import CommandGroup


def abc_str(move: str):
  return [f"Your move was: {move}"]


class MyCommandsLoader(CLICommandsLoader):
  def load_command_table(self, args):
      with CommandGroup(self, 'abc', '__main__#{}') as g:
          g.command('str', 'abc_str')
      return OrderedDict(self.command_table)

  def load_arguments(self, command):
      with ArgumentsContext(self, 'abc str') as ac:
          ac.argument("move", 
              options_list=("--move", "-m"),
              choices=["rock", "paper", "scissors"],
              type=str)
      super(MyCommandsLoader, self).load_arguments(command)


mycli = CLI(cli_name='mycli', commands_loader_cls=MyCommandsLoader)
exit_code = mycli.invoke(sys.argv[1:])
sys.exit(exit_code)

Run the above code like:

> python mycli.py abc str --move fire
mycli abc str: 'fire' is not in the 'mycli abc str' command group. See 'mycli abc str --help'.

Here is the link to the line that is related to the error

if action.choices is not None and value not in action.choices:

Knack version: 0.7.2
Python: 3.8
OS: Windows 10 x64

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