Skip to content

Misleading error message for invalid choice arguments #236

@fralik

Description

@fralik

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions