Skip to content

Conversation

@bwinkers
Copy link

We display the choices pipe delimited and block moving forwards without selecting valid option.

We display the choices pipe delimited and block moving forwards without selecting valid option.
@bwinkers
Copy link
Author

I just wanted to offer up my solution for supporting choices in the parameters.

Copy link
Owner

@multimeric multimeric left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!
I'm not opposed to looping until a valid argument is provided (although I might add the option to disable this later on).
Please also add a test or two that demonstrate this outputs the expected things.


# Delegate to the parent class. Default must be '' in order to get the type function to be called
action = super().add_argument(*args, type=type, default="", **kwargs)
if(choices):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather you extract the kwargs into a dictionary here so that we're not duplicating code.

# If the user provided no value for this argument, prompt them for it
if val == "":
prompt = "{}{}\n> {}".format(self.name, help_str, default_str)
prompt = "{}{}\n> {}".format(self.name, help_str, choices_options)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing default_str here it looks like this breaks the default prompt for non-choice args

getpass.getpass(prompt=prompt) if self.secure else input(prompt)
)

while choices_options and newval not in choices:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind having a loop here, but please refactor it such that the other arguments (without choices) also "loop until valid" (aka, until the self.type(newval) runs without failing).

@multimeric
Copy link
Owner

I also accidentally forgot to run CI builds on pull requests but that should be enabled now, so hopefully it should show if any tests are failing once you push a new commit.

@PanieriLorenzo
Copy link

News? I'd really like this feature

@multimeric
Copy link
Owner

You are welcome to fork this fork and submit another pull request.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants