-
Notifications
You must be signed in to change notification settings - Fork 5
Added support for parameter choices #7
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ class PromptParser(argparse.ArgumentParser): | |
| Extends ArgumentParser to allow any unspecified arguments to be input dynamically on the command line | ||
| """ | ||
|
|
||
| def add_argument(self, *args, prompt=True, secure=False, **kwargs): | ||
| def add_argument(self, *args, prompt=True, choices=False, secure=False, **kwargs): | ||
| """ | ||
| For all unlisted arguments, refer to the parent class | ||
| :param prompt: False if we never want to prompt the user for this argument | ||
|
|
@@ -27,6 +27,7 @@ def add_argument(self, *args, prompt=True, secure=False, **kwargs): | |
| type=kwargs.get("type"), | ||
| secure=secure, | ||
| default=kwargs.get("default"), | ||
| choices=choices | ||
| ) | ||
|
|
||
| # Remove the old type so we can replace it with our own | ||
|
|
@@ -36,7 +37,10 @@ def add_argument(self, *args, prompt=True, secure=False, **kwargs): | |
| del kwargs["default"] | ||
|
|
||
| # 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): | ||
| action = super().add_argument(*args, type=type, choices=choices, default="", **kwargs) | ||
| else: | ||
| action = super().add_argument(*args, type=type, default="", **kwargs) | ||
|
|
||
| # Set the argument name, now that the parser has parsed it | ||
| type.name = action.dest | ||
|
|
@@ -50,7 +54,7 @@ class Prompt: | |
| A class the pretends to be a function so that it can be used as the 'type' argument for the ArgumentParser | ||
| """ | ||
|
|
||
| def __init__(self, name=None, help=None, type=None, default=None, secure=False): | ||
| def __init__(self, name=None, help=None, type=None, default=None, secure=False, choices=False): | ||
| """ | ||
| Creates a new prompt validator | ||
| :param name: The identifier for the variable | ||
|
|
@@ -64,20 +68,36 @@ def __init__(self, name=None, help=None, type=None, default=None, secure=False): | |
| self.help = help | ||
| self.default = default | ||
| self.secure = secure | ||
| self.choices = choices | ||
|
|
||
| def __call__(self, val): | ||
| default_str = f"({self.default}) " | ||
| help_str = "" if self.help is None else f": {self.help}" | ||
|
|
||
| # Make our choices pretty for display | ||
| choices = self.choices | ||
| if type(choices) == list: | ||
| choices_options ="({options}): ".format(options='|'.join(choices)) | ||
| else: | ||
| choices_options = False | ||
|
|
||
| try: | ||
| # 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) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By removing |
||
|
|
||
| newval = ( | ||
| getpass.getpass(prompt=prompt) if self.secure else input(prompt) | ||
| ) | ||
|
|
||
| while choices_options and newval not in choices: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| help_str = "You must choose a {} from the provided options shown below:".format(self.name) | ||
| prompt = "{}\n> {}".format( help_str, choices_options) | ||
|
|
||
| newval = ( | ||
| getpass.getpass(prompt=prompt) if self.secure else input(prompt) | ||
| ) | ||
|
|
||
| # If they just hit enter, they want the default value | ||
| if newval == "": | ||
| newval = self.default | ||
|
|
||
There was a problem hiding this comment.
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.