Skip to content

Commit 6acaeba

Browse files
committed
Add -e/--eval and refactor argparse
1 parent bd2c6cb commit 6acaeba

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

pysrc/juliacall/__main__.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
from juliacall.repl import run_repl
2-
31
if __name__ == '__main__':
4-
run_repl()
2+
import argparse
3+
from pathlib import Path
4+
parser = argparse.ArgumentParser("JuliaCall REPL (experimental)")
5+
parser.add_argument('-e', '--eval', type=str, default=None, help='Evaluate <expr>. If specified, all other arguments are ignored.')
6+
7+
parser.add_argument('--banner', choices=['yes', 'no', 'short'], default='yes', help='Enable or disable startup banner')
8+
parser.add_argument('--quiet', '-q', action='store_true', help='Quiet startup: no banner, suppress REPL warnings')
9+
parser.add_argument('--history-file', choices=['yes', 'no'], default='yes', help='Load or save history')
10+
parser.add_argument('--preamble', type=Path, help='Code to be included before the REPL starts')
11+
args = parser.parse_args()
12+
if args.eval is not None:
13+
from juliacall import Main
14+
Main.seval(args.eval)
15+
else:
16+
from juliacall.repl import run_repl
17+
run_repl(
18+
banner=args.banner,
19+
quiet=args.quiet,
20+
history_file=args.history_file,
21+
preamble=args.preamble
22+
)

pysrc/juliacall/repl.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
def run_repl():
1+
def run_repl(banner='yes', quiet=False, history_file='yes', preamble=None):
22
import os
33
os.environ.setdefault("PYTHON_JULIACALL_HANDLE_SIGNALS", "yes")
44
if os.environ.get("PYTHON_JULIACALL_HANDLE_SIGNALS") != "yes":
55
print("Experimental JuliaCall REPL requires PYTHON_JULIACALL_HANDLE_SIGNALS=yes")
66
exit(1)
77

8-
import argparse
9-
from pathlib import Path
10-
parser = argparse.ArgumentParser("JuliaCall REPL (experimental)")
11-
parser.add_argument('--banner', choices=['yes', 'no', 'short'], default='yes', help='Enable or disable startup banner')
12-
parser.add_argument('--quiet', '-q', action='store_true', help='Quiet startup: no banner, suppress REPL warnings')
13-
parser.add_argument('--history-file', choices=['yes', 'no'], default='yes', help='Load or save history')
14-
parser.add_argument('--preamble', type=Path, help='Code to be included before the REPL starts')
15-
args = parser.parse_args()
16-
178
from juliacall import Main, Base
189

1910
Base.is_interactive = True
2011

21-
if not args.quiet:
12+
if not quiet:
2213
Main.include(os.path.join(os.path.dirname(__file__), 'banner.jl'))
23-
Main.__PythonCall_banner(Base.Symbol(args.banner))
14+
Main.__PythonCall_banner(Base.Symbol(banner))
2415

2516
if Main.seval(r'VERSION > v"v1.11.0-alpha1"'):
2617
no_banner_opt = Base.Symbol("no")
2718
else:
2819
no_banner_opt = False
2920

30-
if args.preamble:
31-
Main.include(str(args.preamble.resolve()))
21+
if preamble:
22+
Main.include(str(preamble.resolve()))
3223

3324
Base.run_main_repl(
3425
Base.is_interactive,
35-
args.quiet,
26+
quiet,
3627
no_banner_opt,
37-
args.history_file == 'yes',
28+
history_file == 'yes',
3829
True
3930
)
4031

0 commit comments

Comments
 (0)