Last active
March 27, 2017 22:23
-
-
Save aranchelk/8adb47d87226942b9466d414b61bc922 to your computer and use it in GitHub Desktop.
Interactive partial function completion prompt for interactive cli tools
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.6 | |
import inspect | |
def cli_prompt_decorator(func): | |
f_spec = inspect.getfullargspec(func) | |
def wrapper(*args): | |
arg_list = list(args) | |
if not len(arg_list) >= len(f_spec.args): | |
print("\nYou have not provided enough arguments from function: %s" % func.__name__) | |
unspecified_args = f_spec.args[len(args):] | |
for a in unspecified_args: | |
new_a_val = input("Please enter a value for argument %s: " % a) | |
arg_type = f_spec.annotations[a] | |
cast_arg = arg_type(new_a_val) | |
arg_list.append(cast_arg) | |
return func(*arg_list) | |
return wrapper | |
@cli_prompt_decorator | |
def print_something(some_string: str): | |
print(some_string) | |
@cli_prompt_decorator | |
def add_two_vals(first_value: int, second_value: int): | |
return first_value + second_value | |
print_something('abcde') | |
print_something() | |
print(add_two_vals(1,2)) | |
print(add_two_vals(1)) | |
print(add_two_vals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment