diff --git a/openbb_terminal/core/completer/choices.py b/openbb_terminal/core/completer/choices.py index 52bca9d01816..db787fceb714 100644 --- a/openbb_terminal/core/completer/choices.py +++ b/openbb_terminal/core/completer/choices.py @@ -95,7 +95,7 @@ def __mock_parse_known_args_and_warn( def __mock_parse_simple_args(parser: ArgumentParser, other_args: List[str]) -> None: """Add the arguments that would have normally added by: - - openbb_terminal.helper_funcs.parse_simple_args + - openbb_terminal.parent_classes.BaseController.parse_simple_args Parameters ---------- @@ -199,8 +199,9 @@ def __patch_controller_functions(controller): ) patcher_list = [ - patch( - target="openbb_terminal.parent_classes.parse_simple_args", + patch.object( + target=controller, + attribute="parse_simple_args", side_effect=__mock_parse_simple_args, return_value=None, ), diff --git a/openbb_terminal/dashboards/dashboards_controller.py b/openbb_terminal/dashboards/dashboards_controller.py index 0b6577074574..12b750412208 100644 --- a/openbb_terminal/dashboards/dashboards_controller.py +++ b/openbb_terminal/dashboards/dashboards_controller.py @@ -14,7 +14,6 @@ from openbb_terminal.menu import session from openbb_terminal.parent_classes import BaseController from openbb_terminal.rich_config import console, MenuText -from openbb_terminal.helper_funcs import parse_simple_args # pylint: disable=consider-using-with @@ -69,146 +68,152 @@ def print_help(self): @log_start_end(log=logger) def call_stocks(self, other_args: List[str]): """Process stocks command""" - create_call_voila(other_args, "stocks", "stocks") + self.create_call_voila(other_args, "stocks", "stocks") @log_start_end(log=logger) def call_correlation(self, other_args: List[str]): """Process correlation command""" - create_call_voila(other_args, "correlation", "correlation") + self.create_call_voila(other_args, "correlation", "correlation") @log_start_end(log=logger) def call_vsurf(self, other_args: List[str]): """Process vsurf command""" - create_call_voila(other_args, "vsurf", "") + self.create_call_voila(other_args, "vsurf", "") @log_start_end(log=logger) def call_chains(self, other_args: List[str]): """Process chains command""" - create_call_voila(other_args, "chains", "") + self.create_call_voila(other_args, "chains", "") @log_start_end(log=logger) def call_shortdata(self, other_args: List[str]): """Process shortdata command""" - create_call_voila(other_args, "shortdata", "") + self.create_call_voila(other_args, "shortdata", "") @log_start_end(log=logger) def call_crypto(self, other_args: List[str]): """Process crypto command""" - create_call_voila(other_args, "crypto", "") + self.create_call_voila(other_args, "crypto", "") @log_start_end(log=logger) def call_futures(self, other_args: List[str]): """Process futures command""" - create_call_voila(other_args, "futures", "") + self.create_call_voila(other_args, "futures", "") @log_start_end(log=logger) def call_forecast(self, other_args: List[str]): """Process forecast command""" - create_call_voila(other_args, "forecast", "") + self.create_call_voila(other_args, "forecast", "") @log_start_end(log=logger) def call_forecasting(self, other_args: List[str]): """Process forecasting command""" - create_call_streamlit(other_args, "forecast") - - -def create_call_voila(other_args: List[str], name: str, filename: str = None) -> None: - filename = filename if filename else name - - parser = argparse.ArgumentParser( - add_help=False, - formatter_class=argparse.ArgumentDefaultsHelpFormatter, - prog=name, - description=f"""Shows {name} dashboard""", - ) - parser.add_argument( - "-j", - "--jupyter", - action="store_true", - default=False, - dest="jupyter", - help="Shows dashboard in jupyter-lab.", - ) - parser.add_argument( - "-n", - "--no-input", - action="store_true", - default=False, - dest="input", - help="Skips confirmation to run server.", - ) - parser.add_argument( - "-d", - "--dark", - action="store_true", - default=False, - dest="dark", - help="Whether to show voila in dark mode", - ) - ns_parser = parse_simple_args(parser, other_args) - - if ns_parser: - cmd = "jupyter-lab" if ns_parser.jupyter else "voila" - base_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "voila") - file = os.path.join(base_path, f"{filename}.ipynb") - if not ns_parser.input: - console.print( - f"Warning: opens a port on your computer to run a {cmd} server." + self.create_call_streamlit(other_args, "forecast") + + @classmethod + def create_call_voila( + cls, other_args: List[str], name: str, filename: str = None + ) -> None: + filename = filename if filename else name + + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog=name, + description=f"""Shows {name} dashboard""", + ) + parser.add_argument( + "-j", + "--jupyter", + action="store_true", + default=False, + dest="jupyter", + help="Shows dashboard in jupyter-lab.", + ) + parser.add_argument( + "-n", + "--no-input", + action="store_true", + default=False, + dest="input", + help="Skips confirmation to run server.", + ) + parser.add_argument( + "-d", + "--dark", + action="store_true", + default=False, + dest="dark", + help="Whether to show voila in dark mode", + ) + ns_parser = cls.parse_simple_args(parser, other_args) + + if ns_parser: + cmd = "jupyter-lab" if ns_parser.jupyter else "voila" + base_path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "voila" ) - response = input("Would you like us to run the server for you [yn]? ") - args = "" - if ns_parser.dark and not ns_parser.jupyter: - args += "--theme=dark" - if ns_parser.input or response.lower() == "y": - cfg.LOGGING_SUPPRESS = True - subprocess.Popen( - f"{cmd} {file} {args}", - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - shell=True, # nosec + file = os.path.join(base_path, f"{filename}.ipynb") + if not ns_parser.input: + console.print( + f"Warning: opens a port on your computer to run a {cmd} server." + ) + response = input("Would you like us to run the server for you [yn]? ") + args = "" + if ns_parser.dark and not ns_parser.jupyter: + args += "--theme=dark" + if ns_parser.input or response.lower() == "y": + cfg.LOGGING_SUPPRESS = True + subprocess.Popen( + f"{cmd} {file} {args}", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + shell=True, # nosec + ) + cfg.LOGGING_SUPPRESS = False + else: + console.print(f"Type: {cmd} voila/{file}\ninto a terminal to run.") + + @classmethod + def create_call_streamlit( + cls, other_args: List[str], name: str, filename: str = None + ) -> None: + filename = filename if filename else name + + parser = argparse.ArgumentParser( + add_help=False, + formatter_class=argparse.ArgumentDefaultsHelpFormatter, + prog=name, + description=f"""Shows streamlit {name} dashboard""", + ) + parser.add_argument( + "-n", + "--no-input", + action="store_true", + default=False, + dest="input", + help="Skips confirmation to run server.", + ) + ns_parser = cls.parse_simple_args(parser, other_args) + + if ns_parser: + cmd = "streamlit run" + base_path = os.path.join( + os.path.abspath(os.path.dirname(__file__)), "stream" ) - cfg.LOGGING_SUPPRESS = False - else: - console.print(f"Type: {cmd} voila/{file}\ninto a terminal to run.") - - -def create_call_streamlit( - other_args: List[str], name: str, filename: str = None -) -> None: - filename = filename if filename else name - - parser = argparse.ArgumentParser( - add_help=False, - formatter_class=argparse.ArgumentDefaultsHelpFormatter, - prog=name, - description=f"""Shows streamlit {name} dashboard""", - ) - parser.add_argument( - "-n", - "--no-input", - action="store_true", - default=False, - dest="input", - help="Skips confirmation to run server.", - ) - ns_parser = parse_simple_args(parser, other_args) - - if ns_parser: - cmd = "streamlit run" - base_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), "stream") - file = os.path.join(base_path, f"{filename}.py") - if not ns_parser.input: - console.print( - f"Warning: opens a port on your computer to run a {cmd} server." - ) - response = input("Would you like us to run the server for you [yn]? ") - args = "" - if ns_parser.input or response.lower() == "y": - subprocess.Popen( - f"{cmd} {file} {args}", - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - shell=True, - ) - else: - console.print(f"Type: {cmd} stream/{file}\ninto a terminal to run.") + file = os.path.join(base_path, f"{filename}.py") + if not ns_parser.input: + console.print( + f"Warning: opens a port on your computer to run a {cmd} server." + ) + response = input("Would you like us to run the server for you [yn]? ") + args = "" + if ns_parser.input or response.lower() == "y": + subprocess.Popen( + f"{cmd} {file} {args}", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + shell=True, + ) + else: + console.print(f"Type: {cmd} stream/{file}\ninto a terminal to run.") diff --git a/openbb_terminal/helper_funcs.py b/openbb_terminal/helper_funcs.py index f50245cae096..45e1a1bfd969 100644 --- a/openbb_terminal/helper_funcs.py +++ b/openbb_terminal/helper_funcs.py @@ -966,47 +966,6 @@ def patch_pandas_text_adjustment(): pandas.io.formats.format.TextAdjustment.adjoin = text_adjustment_adjoin -def parse_simple_args(parser: argparse.ArgumentParser, other_args: List[str]): - """Parse list of arguments into the supplied parser. - - Parameters - ---------- - parser: argparse.ArgumentParser - Parser with predefined arguments - other_args: List[str] - List of arguments to parse - - Returns - ------- - ns_parser: - Namespace with parsed arguments - """ - parser.add_argument( - "-h", "--help", action="store_true", help="show this help message" - ) - - if obbff.USE_CLEAR_AFTER_CMD: - system_clear() - - try: - (ns_parser, l_unknown_args) = parser.parse_known_args(other_args) - except SystemExit: - # In case the command has required argument that isn't specified - console.print("\n") - return None - - if ns_parser.help: - txt_help = parser.format_help() - console.print(f"[help]{txt_help}[/help]") - return None - - if l_unknown_args: - console.print(f"The following args couldn't be interpreted: {l_unknown_args}") - console.print("\n") - - return ns_parser - - def lambda_financials_colored_values(val: str) -> str: """Add a color to a value.""" if val == "N/A" or str(val) == "nan": diff --git a/openbb_terminal/keys_controller.py b/openbb_terminal/keys_controller.py index dec773917941..1337590ec73a 100644 --- a/openbb_terminal/keys_controller.py +++ b/openbb_terminal/keys_controller.py @@ -14,10 +14,7 @@ from openbb_terminal.core.config.paths import USER_ENV_FILE from openbb_terminal.custom_prompt_toolkit import NestedCompleter from openbb_terminal.decorators import log_start_end -from openbb_terminal.helper_funcs import ( - EXPORT_ONLY_RAW_DATA_ALLOWED, - parse_simple_args, -) +from openbb_terminal.helper_funcs import EXPORT_ONLY_RAW_DATA_ALLOWED from openbb_terminal.menu import session from openbb_terminal.parent_classes import BaseController from openbb_terminal.rich_config import console, MenuText, translate @@ -136,7 +133,7 @@ def call_av(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["av"] = keys_model.set_av_key( key=ns_parser.key, persist=True, show_output=True @@ -166,7 +163,7 @@ def call_fmp(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["fmp"] = keys_model.set_fmp_key( key=ns_parser.key, persist=True, show_output=True @@ -194,7 +191,7 @@ def call_quandl(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["quandl"] = keys_model.set_quandl_key( key=ns_parser.key, persist=True, show_output=True @@ -222,7 +219,7 @@ def call_polygon(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["polygon"] = keys_model.set_polygon_key( key=ns_parser.key, persist=True, show_output=True @@ -250,7 +247,7 @@ def call_fred(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["fred"] = keys_model.set_fred_key( key=ns_parser.key, persist=True, show_output=True @@ -278,7 +275,7 @@ def call_news(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["news"] = keys_model.set_news_key( key=ns_parser.key, persist=True, show_output=True @@ -306,7 +303,7 @@ def call_tradier(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["tradier"] = keys_model.set_tradier_key( key=ns_parser.key, persist=True, show_output=True @@ -333,7 +330,7 @@ def call_cmc(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["cmc"] = keys_model.set_cmc_key( key=ns_parser.key, persist=True, show_output=True @@ -360,7 +357,7 @@ def call_finnhub(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["finnhub"] = keys_model.set_finnhub_key( key=ns_parser.key, persist=True, show_output=True @@ -387,7 +384,7 @@ def call_iex(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["iex"] = keys_model.set_iex_key( key=ns_parser.key, persist=True, show_output=True @@ -446,7 +443,7 @@ def call_reddit(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://www.reddit.com\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: slash_components = "".join([f"/{val}" for val in self.queue]) @@ -500,7 +497,7 @@ def call_twitter(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://developer.twitter.com\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["twitter"] = keys_model.set_twitter_key( key=ns_parser.key, @@ -536,7 +533,7 @@ def call_rh(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://robinhood.com/us/en/\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["rh"] = keys_model.set_rh_key( username=ns_parser.username, @@ -581,7 +578,7 @@ def call_degiro(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://www.degiro.fr\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["degiro"] = keys_model.set_degiro_key( username=ns_parser.username, @@ -624,7 +621,7 @@ def call_oanda(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://developer.oanda.com\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["oanda"] = keys_model.set_oanda_key( account=ns_parser.account, @@ -662,7 +659,7 @@ def call_binance(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://binance.com\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["binance"] = keys_model.set_binance_key( key=ns_parser.key, @@ -692,7 +689,7 @@ def call_bitquery(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["bitquery"] = keys_model.set_bitquery_key( key=ns_parser.key, persist=True, show_output=True @@ -719,7 +716,7 @@ def call_si(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["si"] = keys_model.set_si_key( key=ns_parser.key, persist=True, show_output=True @@ -761,7 +758,7 @@ def call_coinbase(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://docs.pro.coinbase.com/\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["coinbase"] = keys_model.set_coinbase_key( key=ns_parser.key, @@ -792,7 +789,7 @@ def call_walert(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["walert"] = keys_model.set_walert_key( key=ns_parser.key, persist=True, show_output=True @@ -821,7 +818,7 @@ def call_glassnode(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["glassnode"] = keys_model.set_glassnode_key( key=ns_parser.key, persist=True, show_output=True @@ -850,7 +847,7 @@ def call_coinglass(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["coinglass"] = keys_model.set_coinglass_key( key=ns_parser.key, persist=True, show_output=True @@ -879,7 +876,7 @@ def call_cpanic(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["cpanic"] = keys_model.set_cpanic_key( key=ns_parser.key, persist=True, show_output=True @@ -908,7 +905,7 @@ def call_ethplorer(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["ethplorer"] = keys_model.set_ethplorer_key( key=ns_parser.key, persist=True, show_output=True @@ -942,7 +939,7 @@ def call_smartstake(self, other_args: List[str]): if not other_args: console.print("For your API Key, visit: https://www.smartstake.io\n") return - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["smartstake"] = keys_model.set_smartstake_key( @@ -976,7 +973,7 @@ def call_github(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["github"] = keys_model.set_github_key( key=ns_parser.key, persist=True, show_output=True @@ -1004,7 +1001,7 @@ def call_messari(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["messari"] = keys_model.set_messari_key( key=ns_parser.key, persist=True, show_output=True @@ -1033,7 +1030,7 @@ def call_eodhd(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["eodhd"] = keys_model.set_eodhd_key( key=ns_parser.key, persist=True, show_output=True @@ -1063,7 +1060,7 @@ def call_santiment(self, other_args: List[str]): return if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["santiment"] = keys_model.set_santiment_key( key=ns_parser.key, persist=True, show_output=True @@ -1093,7 +1090,7 @@ def call_shroom(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["shroom"] = keys_model.set_shroom_key( key=ns_parser.key, persist=True, show_output=True @@ -1121,7 +1118,7 @@ def call_tokenterminal(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["tokenterminal"] = keys_model.set_tokenterminal_key( key=ns_parser.key, persist=True, show_output=True @@ -1151,7 +1148,7 @@ def call_stocksera(self, other_args: List[str]): if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-k") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: self.status_dict["stocksera"] = keys_model.set_stocksera_key( key=ns_parser.key, persist=True, show_output=True diff --git a/openbb_terminal/parent_classes.py b/openbb_terminal/parent_classes.py index d85d7d2d4b66..164ffa0237e9 100644 --- a/openbb_terminal/parent_classes.py +++ b/openbb_terminal/parent_classes.py @@ -35,7 +35,6 @@ get_flair, load_json, parse_and_split_input, - parse_simple_args, prefill_form, screenshot, search_wikipedia, @@ -470,7 +469,7 @@ def call_resources(self, other_args: List[str]) -> None: prog="resources", description="Display available markdown resources.", ) - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if os.path.isfile(self.FILE_PATH): @@ -531,7 +530,7 @@ def call_support(self, other_args: List[str]) -> None: if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-c") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: prefill_form( @@ -563,7 +562,7 @@ def call_glossary(self, other_args: List[str]) -> None: if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-w") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) glossary_file = os.path.join(os.path.dirname(__file__), "glossary.json") glossary_dict = load_json(glossary_file) @@ -600,7 +599,7 @@ def call_wiki(self, other_args: List[str]) -> None: if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-e") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if ns_parser.expression: @@ -627,7 +626,7 @@ def call_record(self, other_args) -> None: ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-r") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: global SESSION_RECORDED_NAME @@ -690,13 +689,56 @@ def call_screenshot(self, other_args: List[str]) -> None: "Default target is plot if there is one open, otherwise it's terminal window. " " In case the user wants the terminal window, it can be forced with '-t` or '--terminal' flag passed.", ) - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: screenshot() + @staticmethod + def parse_simple_args(parser: argparse.ArgumentParser, other_args: List[str]): + """Parse list of arguments into the supplied parser. + + Parameters + ---------- + parser: argparse.ArgumentParser + Parser with predefined arguments + other_args: List[str] + List of arguments to parse + + Returns + ------- + ns_parser: + Namespace with parsed arguments + """ + parser.add_argument( + "-h", "--help", action="store_true", help="show this help message" + ) + + if obbff.USE_CLEAR_AFTER_CMD: + system_clear() + + try: + (ns_parser, l_unknown_args) = parser.parse_known_args(other_args) + except SystemExit: + # In case the command has required argument that isn't specified + console.print("\n") + return None + + if ns_parser.help: + txt_help = parser.format_help() + console.print(f"[help]{txt_help}[/help]") + return None + + if l_unknown_args: + console.print( + f"The following args couldn't be interpreted: {l_unknown_args}\n" + ) + + return ns_parser + + @classmethod def parse_known_args_and_warn( - self, + cls, parser: argparse.ArgumentParser, other_args: List[str], export_allowed: int = NO_EXPORT, @@ -766,7 +808,7 @@ def parse_known_args_and_warn( help="Number of entries to show in data.", type=check_positive, ) - sources = get_ordered_list_sources(f"{self.PATH}{parser.prog}") + sources = get_ordered_list_sources(f"{cls.PATH}{parser.prog}") # Allow to change source if there is more than one if len(sources) > 1: parser.add_argument( diff --git a/openbb_terminal/reports/reports_controller.py b/openbb_terminal/reports/reports_controller.py index 5a52d23205e4..d5710671f49e 100644 --- a/openbb_terminal/reports/reports_controller.py +++ b/openbb_terminal/reports/reports_controller.py @@ -13,7 +13,6 @@ from openbb_terminal.core.config.paths import ( USER_CUSTOM_REPORTS_DIRECTORY, ) -from openbb_terminal.helper_funcs import parse_simple_args from openbb_terminal.reports import reports_model from openbb_terminal.custom_prompt_toolkit import NestedCompleter from openbb_terminal.decorators import log_start_end @@ -202,7 +201,7 @@ def run_report(self, report_name: str, other_args: List[str]): other_args.insert( 0, "--" + list(self.PARAMETERS_DICT[report_name].keys())[0] ) - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: cfg.LOGGING_SUPPRESS = True diff --git a/openbb_terminal/settings_controller.py b/openbb_terminal/settings_controller.py index 9e48bfe1582b..29b898790b10 100644 --- a/openbb_terminal/settings_controller.py +++ b/openbb_terminal/settings_controller.py @@ -21,7 +21,6 @@ from openbb_terminal.decorators import log_start_end from openbb_terminal.helper_funcs import ( get_flair, - parse_simple_args, get_user_timezone_or_invalid, replace_user_timezone, set_user_data_folder, @@ -175,7 +174,7 @@ def call_source(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: try: @@ -224,7 +223,7 @@ def call_dpi(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser and ns_parser.value: set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_DPI", str(ns_parser.value)) cfg_plot.PLOT_DPI = ns_parser.value @@ -248,7 +247,7 @@ def call_height(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_HEIGHT", str(ns_parser.value)) cfg_plot.PLOT_HEIGHT = ns_parser.value @@ -272,7 +271,7 @@ def call_width(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key(obbff.USER_ENV_FILE, "OPENBB_PLOT_WIDTH", str(ns_parser.value)) cfg_plot.PLOT_WIDTH = ns_parser.value @@ -295,7 +294,7 @@ def call_pheight(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key( obbff.USER_ENV_FILE, @@ -322,7 +321,7 @@ def call_pwidth(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key( obbff.USER_ENV_FILE, @@ -349,7 +348,7 @@ def call_monitor(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key(obbff.USER_ENV_FILE, "OPENBB_MONITOR", str(ns_parser.value)) cfg_plot.MONITOR = ns_parser.value @@ -372,7 +371,7 @@ def call_backend(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: set_key(obbff.USER_ENV_FILE, "OPENBB_BACKEND", str(ns_parser.value)) if ns_parser.value == "None": @@ -400,7 +399,7 @@ def call_lang(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-v") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if ns_parser.value: set_key( @@ -433,7 +432,7 @@ def call_tz(self, other_args: List[str]): if other_args and "-t" not in other_args[0]: other_args.insert(0, "-t") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if ns_parser.timezone: replace_user_timezone(ns_parser.timezone.replace("_", "/", 1)) @@ -457,7 +456,7 @@ def call_flair(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-e") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if not ns_parser.emoji: ns_parser.emoji = "" @@ -484,7 +483,7 @@ def call_userdata(self, other_args: List[str]): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "--folder") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: if other_args or self.queue: diff --git a/openbb_terminal/sources_controller.py b/openbb_terminal/sources_controller.py index 2a2def15e47c..8853723dfedf 100644 --- a/openbb_terminal/sources_controller.py +++ b/openbb_terminal/sources_controller.py @@ -19,7 +19,6 @@ from openbb_terminal.menu import session from openbb_terminal.parent_classes import BaseController from openbb_terminal.rich_config import console, MenuText -from openbb_terminal.helper_funcs import parse_simple_args # pylint: disable=too-many-lines,no-member,too-many-public-methods,C0302 # pylint:disable=import-outside-toplevel @@ -121,7 +120,7 @@ def call_get(self, other_args): ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-c") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: try: the_item = self.commands_with_sources[ns_parser.cmd] @@ -171,7 +170,7 @@ def call_set(self, other_args): other_args.insert(0, "-c") if "-s" not in other_args and "--source" not in other_args: other_args.insert(2, "-s") - ns_parser = parse_simple_args(parser, other_args) + ns_parser = self.parse_simple_args(parser, other_args) if ns_parser: menus = ns_parser.cmd.split("_") num_menus = len(menus) diff --git a/openbb_terminal/terminal_controller.py b/openbb_terminal/terminal_controller.py index b36a1ff267ce..ce9f2dfcc934 100644 --- a/openbb_terminal/terminal_controller.py +++ b/openbb_terminal/terminal_controller.py @@ -39,7 +39,6 @@ from openbb_terminal.helper_funcs import ( check_positive, get_flair, - parse_simple_args, EXPORT_ONLY_RAW_DATA_ALLOWED, ) from openbb_terminal.loggers import setup_logging @@ -250,7 +249,7 @@ def call_guess(self, other_args: List[str]) -> None: ) if other_args and "-" not in other_args[0][0]: other_args.insert(0, "-l") - ns_parser_guess = parse_simple_args(parser_exe, other_args) + ns_parser_guess = self.parse_simple_args(parser_exe, other_args) if self.GUESS_TOTAL_TRIES == 0: self.GUESS_NUMBER_TRIES_LEFT = ns_parser_guess.limit @@ -696,7 +695,7 @@ def call_exe(self, other_args: List[str]): ) if args and "-" not in args[0][0]: args.insert(0, "--file") - ns_parser_exe = parse_simple_args(parser_exe, args) + ns_parser_exe = self.parse_simple_args(parser_exe, args) if ns_parser_exe: if ns_parser_exe.path: if ns_parser_exe.path in self.ROUTINE_CHOICES: