Skip to content

Commit e76cafd

Browse files
committed
Make CLI options more like wolframscript
1 parent b176302 commit e76cafd

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

mathicsscript/__main__.py

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def load_settings(shell):
122122
if query is None:
123123
continue
124124
evaluation.evaluate(query)
125-
except (KeyboardInterrupt):
125+
except KeyboardInterrupt:
126126
print("\nKeyboardInterrupt")
127127
return True
128128

@@ -223,7 +223,7 @@ def fmt_fun(query: Any) -> Any:
223223
# FIXME add this... when in Mathics core updated
224224
# shell.definitions.increment_line(1)
225225

226-
except (KeyboardInterrupt):
226+
except KeyboardInterrupt:
227227
print("\nKeyboardInterrupt")
228228
except EOFError:
229229
if prompt:
@@ -240,10 +240,7 @@ def fmt_fun(query: Any) -> Any:
240240
shell.reset_lineno()
241241

242242

243-
if click.__version__ >= "7.":
244-
case_sensitive = {"case_sensitive": False}
245-
else:
246-
case_sensitive = {}
243+
case_sensitive = {"case_sensitive": False}
247244

248245

249246
@click.command()
@@ -256,7 +253,7 @@ def fmt_fun(query: Any) -> Any:
256253
@click.version_option(version=__version__)
257254
@click.option(
258255
"--full-form",
259-
"-f",
256+
"-F",
260257
"full_form",
261258
flag_value="full_form",
262259
default=False,
@@ -296,10 +293,11 @@ def fmt_fun(query: Any) -> Any:
296293
),
297294
)
298295
@click.option(
299-
"--unicode/--no-unicode",
296+
"-charset",
297+
metavar="ENCODING",
300298
default=sys.getdefaultencoding() == "utf-8",
301299
show_default=True,
302-
help="Accept Unicode operators in input and show unicode in output.",
300+
help="Use encoding for output. Encodings can be any entry in $CharacterEncodings.",
303301
)
304302
@click.option(
305303
"--post-mortem/--no-post-mortem",
@@ -321,20 +319,19 @@ def fmt_fun(query: Any) -> Any:
321319
)
322320
@click.option(
323321
"-c",
324-
"-e",
325-
"--execute",
326-
help="evaluate EXPR before processing any input files (may be given "
327-
"multiple times). Sets --quiet and --no-completion",
322+
"-code",
323+
"--code",
324+
help="Give Mathics3 source code to execute. This may be given "
325+
"multiple times. Sets --quiet and --no-completion",
328326
multiple=True,
329327
required=False,
330328
)
331329
@click.option(
332-
"--run",
330+
"-f",
331+
"-file",
332+
"--file",
333333
type=click.Path(readable=True),
334-
help=(
335-
"go to interactive shell after evaluating PATH but leave "
336-
"history empty and set $Line to 1"
337-
),
334+
help=("Give a file containing Mathics3 source code to execute."),
338335
)
339336
@click.option(
340337
"-s",
@@ -383,18 +380,17 @@ def main(
383380
quiet,
384381
readline,
385382
completion,
386-
unicode,
383+
charset,
387384
post_mortem,
388385
prompt,
389386
pyextensions,
390-
execute,
391-
run,
387+
code,
388+
file,
392389
style,
393390
pygments_tokens,
394391
strict_wl_output,
395392
asymptote,
396393
matplotlib,
397-
file,
398394
) -> int:
399395
"""A command-line interface to Mathics.
400396
@@ -433,20 +429,20 @@ def main(
433429
else:
434430
sys.excepthook = post_mortem_excepthook
435431

436-
readline = "none" if (execute or file and not persist) else readline.lower()
432+
readline = "none" if (code or file and not persist) else readline.lower()
437433
if readline == "prompt":
438434
shell = TerminalShellPromptToolKit(
439-
definitions, style, completion, unicode, prompt, edit_mode
435+
definitions, style, completion, charset, prompt, edit_mode
440436
)
441437
else:
442438
want_readline = readline == "gnu"
443439
shell = TerminalShellGNUReadline(
444-
definitions, style, want_readline, completion, unicode, prompt
440+
definitions, style, want_readline, completion, charset, prompt
445441
)
446442

447443
load_settings(shell)
448-
if run:
449-
with open(run, "r") as ifile:
444+
if file:
445+
with open(file, "r") as ifile:
450446
feeder = MathicsFileLineFeeder(ifile)
451447
try:
452448
while not feeder.empty():
@@ -460,13 +456,13 @@ def main(
460456
if query is None:
461457
continue
462458
evaluation.evaluate(query, timeout=settings.TIMEOUT)
463-
except (KeyboardInterrupt):
459+
except KeyboardInterrupt:
464460
print("\nKeyboardInterrupt")
465461

466462
definitions.set_line_no(0)
467463

468-
if execute:
469-
for expr in execute:
464+
if code:
465+
for expr in code:
470466
evaluation = Evaluation(
471467
shell.definitions, output=TerminalOutput(shell), format="text"
472468
)
@@ -503,7 +499,7 @@ def main(
503499
if query is None:
504500
continue
505501
evaluation.evaluate(query, timeout=settings.TIMEOUT)
506-
except (KeyboardInterrupt):
502+
except KeyboardInterrupt:
507503
print("\nKeyboardInterrupt")
508504

509505
if not persist:
@@ -531,7 +527,7 @@ def main(
531527

532528
definitions.set_line_no(0)
533529
interactive_eval_loop(
534-
shell, unicode, prompt, asymptote, matplotlib, strict_wl_output
530+
shell, charset, prompt, asymptote, matplotlib, strict_wl_output
535531
)
536532
return exit_rc
537533

test/test_returncode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def get_testdir():
99

1010

1111
def test_returncode():
12-
assert subprocess.run(["mathicsscript", "-e", "Quit[5]"]).returncode == 5
13-
assert subprocess.run(["mathicsscript", "-e", "1 + 2'"]).returncode == 0
14-
assert subprocess.run(["mathicsscript", "-e", "Quit[0]"]).returncode == 0
12+
assert subprocess.run(["mathicsscript", "-c", "Quit[5]"]).returncode == 5
13+
assert subprocess.run(["mathicsscript", "-c", "1 + 2'"]).returncode == 0
14+
assert subprocess.run(["mathicsscript", "-c", "Quit[0]"]).returncode == 0
1515

1616
gcd_file = osp.join(get_testdir(), "data", "recursive-gcd.m")
1717
assert subprocess.run(["mathicsscript", "-f", gcd_file]).returncode == 0

0 commit comments

Comments
 (0)