diff --git a/binarycpython/utils/functions.py b/binarycpython/utils/functions.py index 127772da6c3a43f7a2d187b8ba277025893ab387..675441579f7d9638ea3ed5e5a91f912bcc824d39 100644 --- a/binarycpython/utils/functions.py +++ b/binarycpython/utils/functions.py @@ -380,7 +380,7 @@ def get_arg_keys(): return get_defaults().keys() -def get_help(param_name, print_help=True, return_dict=False, fail_silently=False): +def get_help(param_name="", print_help=True, return_dict=False, fail_silently=False): """ Function that returns the help info for a given parameter. @@ -397,78 +397,82 @@ def get_help(param_name, print_help=True, return_dict=False, fail_silently=False return_dict: wether to return the help info dictionary """ - + available_arg_keys = get_arg_keys() - if param_name in available_arg_keys: - help_info = binary_c_python_api.return_help(param_name) - cleaned = [el for el in help_info.split("\n") if not el == ""] - - # Get line numbers - did_you_mean_nr = [ - i for i, el in enumerate(cleaned) if el.startswith("Did you mean") - ] - parameter_line_nr = [ - i for i, el in enumerate(cleaned) if el.startswith("binary_c help") - ] - default_line_nr = [ - i for i, el in enumerate(cleaned) if el.startswith("Default") - ] - macros_line_nr = [ - i for i, el in enumerate(cleaned) if el.startswith("Available") - ] - - help_info_dict = {} - - # Get alternatives - if did_you_mean_nr: - alternatives = cleaned[did_you_mean_nr[0] + 1 : parameter_line_nr[0]] - alternatives = [el.strip() for el in alternatives] - help_info_dict["alternatives"] = alternatives - - # Information about the parameter - parameter_line = cleaned[parameter_line_nr[0]] - parameter_name = parameter_line.split(":")[1].strip().split(" ")[0] - parameter_value_input_type = ( - " ".join(parameter_line.split(":")[1].strip().split(" ")[1:]) - .replace("<", "") - .replace(">", "") - ) + if not param_name: + print("Please set the param_name to any of the following:\n {}".format(sorted(available_arg_keys))) + return None + else: + if param_name in available_arg_keys: + help_info = binary_c_python_api.return_help(param_name) + cleaned = [el for el in help_info.split("\n") if not el == ""] + + # Get line numbers + did_you_mean_nr = [ + i for i, el in enumerate(cleaned) if el.startswith("Did you mean") + ] + parameter_line_nr = [ + i for i, el in enumerate(cleaned) if el.startswith("binary_c help") + ] + default_line_nr = [ + i for i, el in enumerate(cleaned) if el.startswith("Default") + ] + macros_line_nr = [ + i for i, el in enumerate(cleaned) if el.startswith("Available") + ] + + help_info_dict = {} + + # Get alternatives + if did_you_mean_nr: + alternatives = cleaned[did_you_mean_nr[0] + 1 : parameter_line_nr[0]] + alternatives = [el.strip() for el in alternatives] + help_info_dict["alternatives"] = alternatives + + # Information about the parameter + parameter_line = cleaned[parameter_line_nr[0]] + parameter_name = parameter_line.split(":")[1].strip().split(" ")[0] + parameter_value_input_type = ( + " ".join(parameter_line.split(":")[1].strip().split(" ")[1:]) + .replace("<", "") + .replace(">", "") + ) - help_info_dict["parameter_name"] = parameter_name - help_info_dict["parameter_value_input_type"] = parameter_value_input_type + help_info_dict["parameter_name"] = parameter_name + help_info_dict["parameter_value_input_type"] = parameter_value_input_type - description_line = " ".join( - cleaned[parameter_line_nr[0] + 1 : default_line_nr[0]] - ) - help_info_dict["description"] = description_line + description_line = " ".join( + cleaned[parameter_line_nr[0] + 1 : default_line_nr[0]] + ) + help_info_dict["description"] = description_line - # Default: - default_line = cleaned[default_line_nr[0]] - default_value = default_line.split(":")[-1].strip() + # Default: + default_line = cleaned[default_line_nr[0]] + default_value = default_line.split(":")[-1].strip() - help_info_dict["default"] = default_value + help_info_dict["default"] = default_value - # Get Macros: - if macros_line_nr: - macros = cleaned[macros_line_nr[0] + 1 :] - help_info_dict["macros"] = macros + # Get Macros: + if macros_line_nr: + macros = cleaned[macros_line_nr[0] + 1 :] + help_info_dict["macros"] = macros - if print_help: - for key in help_info_dict.keys(): - print("{}:\n\t{}".format(key, help_info_dict[key])) + if print_help: + for key in help_info_dict.keys(): + print("{}:\n\t{}".format(key, help_info_dict[key])) - if return_dict: - return help_info_dict + if return_dict: + return help_info_dict - else: - if not fail_silently: - print( - "{} is not a valid parameter name. Please choose from the following parameters:\n\t{}".format( - param_name, list(available_arg_keys) + else: + if not fail_silently: + print( + "{} is not a valid parameter name. Please choose from the following parameters:\n\t{}".format( + param_name, list(available_arg_keys) + ) ) - ) - return None + return None def run_system(**kwargs):