diff --git a/mathicsscript/__main__.py b/mathicsscript/__main__.py index 8267be2..b88e6b2 100755 --- a/mathicsscript/__main__.py +++ b/mathicsscript/__main__.py @@ -122,7 +122,7 @@ def load_settings(shell): if query is None: continue evaluation.evaluate(query) - except (KeyboardInterrupt): + except KeyboardInterrupt: print("\nKeyboardInterrupt") return True @@ -223,7 +223,7 @@ def fmt_fun(query: Any) -> Any: # FIXME add this... when in Mathics core updated # shell.definitions.increment_line(1) - except (KeyboardInterrupt): + except KeyboardInterrupt: print("\nKeyboardInterrupt") except EOFError: if prompt: @@ -240,13 +240,10 @@ def fmt_fun(query: Any) -> Any: shell.reset_lineno() -if click.__version__ >= "7.": - case_sensitive = {"case_sensitive": False} -else: - case_sensitive = {} +case_sensitive = {"case_sensitive": False} -@click.command() +@click.command(context_settings=dict(help_option_names=["-h", "-help", "--help"])) @click.option( "--edit-mode", "-e", @@ -256,7 +253,7 @@ def fmt_fun(query: Any) -> Any: @click.version_option(version=__version__) @click.option( "--full-form", - "-f", + "-F", "full_form", flag_value="full_form", default=False, @@ -296,10 +293,11 @@ def fmt_fun(query: Any) -> Any: ), ) @click.option( - "--unicode/--no-unicode", + "-charset", + metavar="ENCODING", default=sys.getdefaultencoding() == "utf-8", show_default=True, - help="Accept Unicode operators in input and show unicode in output.", + help="Use encoding for output. Encodings can be any entry in $CharacterEncodings.", ) @click.option( "--post-mortem/--no-post-mortem", @@ -321,20 +319,19 @@ def fmt_fun(query: Any) -> Any: ) @click.option( "-c", - "-e", - "--execute", - help="evaluate EXPR before processing any input files (may be given " - "multiple times). Sets --quiet and --no-completion", + "-code", + "--code", + help="Give Mathics3 source code to execute. This may be given " + "multiple times. Sets --quiet and --no-completion", multiple=True, required=False, ) @click.option( - "--run", + "-f", + "-file", + "--file", type=click.Path(readable=True), - help=( - "go to interactive shell after evaluating PATH but leave " - "history empty and set $Line to 1" - ), + help=("Give a file containing Mathics3 source code to execute."), ) @click.option( "-s", @@ -383,18 +380,17 @@ def main( quiet, readline, completion, - unicode, + charset, post_mortem, prompt, pyextensions, - execute, - run, + code, + file, style, pygments_tokens, strict_wl_output, asymptote, matplotlib, - file, ) -> int: """A command-line interface to Mathics. @@ -433,20 +429,20 @@ def main( else: sys.excepthook = post_mortem_excepthook - readline = "none" if (execute or file and not persist) else readline.lower() + readline = "none" if (code or file and not persist) else readline.lower() if readline == "prompt": shell = TerminalShellPromptToolKit( - definitions, style, completion, unicode, prompt, edit_mode + definitions, style, completion, charset, prompt, edit_mode ) else: want_readline = readline == "gnu" shell = TerminalShellGNUReadline( - definitions, style, want_readline, completion, unicode, prompt + definitions, style, want_readline, completion, charset, prompt ) load_settings(shell) - if run: - with open(run, "r") as ifile: + if file: + with open(file, "r") as ifile: feeder = MathicsFileLineFeeder(ifile) try: while not feeder.empty(): @@ -460,13 +456,13 @@ def main( if query is None: continue evaluation.evaluate(query, timeout=settings.TIMEOUT) - except (KeyboardInterrupt): + except KeyboardInterrupt: print("\nKeyboardInterrupt") definitions.set_line_no(0) - if execute: - for expr in execute: + if code: + for expr in code: evaluation = Evaluation( shell.definitions, output=TerminalOutput(shell), format="text" ) @@ -503,7 +499,7 @@ def main( if query is None: continue evaluation.evaluate(query, timeout=settings.TIMEOUT) - except (KeyboardInterrupt): + except KeyboardInterrupt: print("\nKeyboardInterrupt") if not persist: @@ -531,7 +527,7 @@ def main( definitions.set_line_no(0) interactive_eval_loop( - shell, unicode, prompt, asymptote, matplotlib, strict_wl_output + shell, charset, prompt, asymptote, matplotlib, strict_wl_output ) return exit_rc diff --git a/pyproject.toml b/pyproject.toml index b71f529..3efcab3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ description = "Command-line interface to Mathics3" dependencies = [ "Mathics_Scanner>1.4.1", "Mathics3 >= 8.0.1", - "click", + "click >= 8.0.0", "colorama", "columnize", "networkx", diff --git a/test/test_returncode.py b/test/test_returncode.py index 8dd5508..2b6a300 100644 --- a/test/test_returncode.py +++ b/test/test_returncode.py @@ -9,12 +9,20 @@ def get_testdir(): def test_returncode(): - assert subprocess.run(["mathicsscript", "-e", "Quit[5]"]).returncode == 5 - assert subprocess.run(["mathicsscript", "-e", "1 + 2'"]).returncode == 0 - assert subprocess.run(["mathicsscript", "-e", "Quit[0]"]).returncode == 0 + assert subprocess.run(["mathicsscript", "-c", "Quit[5]"]).returncode == 5 + assert subprocess.run(["mathicsscript", "-c", "1 + 2'"]).returncode == 0 + assert subprocess.run(["mathicsscript", "-c", "Quit[0]"]).returncode == 0 gcd_file = osp.join(get_testdir(), "data", "recursive-gcd.m") - assert subprocess.run(["mathicsscript", "-f", gcd_file]).returncode == 0 + # We add --readline None for OSX and Windows which don't interact well + # with tty's in a CI environment. TODO: separate out Ubuntu and + # use prompt_readline for that? + assert ( + subprocess.run( + ["mathicsscript", "--readline", "None", "-f", gcd_file] + ).returncode + == 0 + ) if __name__ == "__main__":