Skip to content

Commit 76ee377

Browse files
author
Release Manager
committed
gh-40872: cli: Allow consuming arguments from the command line when running a file This fixes a regression with respect to the old sage sh script. ``` $ cat add.sage import sys a = int(sys.argv[1]) b = int(sys.argv[2]) print(a, "+", b, "=", a+b) ``` Before this: ``` $ sage add.sage 1 1 usage: sage [-h] [-v] [-q] [--simple-prompt] [-V] [-n [{jupyter,jupyterlab}]] [-c [COMMAND]] [file] sage: error: unrecognized arguments: 1 1 ``` After this (or with the old sage script): ``` $ sage add.sage 1 1 1 + 1 = 2 ``` ### ⌛ Dependencies #40755 Fixes: #40871 URL: #40872 Reported by: Antonio Rojas Reviewer(s): Tobias Diez
2 parents c1228b2 + cbbe2c1 commit 76ee377

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/sage/cli/options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ class CliOptions:
2323
command: str | None = None
2424

2525
"""The file to execute."""
26-
file: str | None = None
26+
file: list[str] | None = None

src/sage/cli/run_file_cmd.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def extend_parser(parser: argparse.ArgumentParser):
2323
"""
2424
parser.add_argument(
2525
"file",
26-
nargs="?",
26+
nargs="*",
2727
help="execute the given file as sage code",
2828
)
2929

@@ -32,13 +32,18 @@ def __init__(self, options: CliOptions):
3232
Initialize the command.
3333
"""
3434
self.options = options
35+
# shift sys.argv for compatibility with the old sage bash script and python command when consuming arguments from the command line
36+
import sys
37+
del sys.argv[0]
3538

3639
def run(self) -> int:
3740
r"""
3841
Execute the given command.
3942
"""
40-
input_file = preparse_file_named(self.options.file) if self.options.file.endswith('.sage') else self.options.file
41-
if self.options.file.endswith('.pyx') or self.options.file.endswith('.spyx'):
43+
input_file = self.options.file[0]
44+
if input_file.endswith('.sage'):
45+
input_file = str(preparse_file_named(input_file))
46+
if input_file.endswith('.pyx') or input_file.endswith('.spyx'):
4247
s = load_cython(input_file)
4348
eval(compile(s, tmp_filename(), 'exec'), sage_globals())
4449
else:

src/sage/cli/run_file_cmd_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from sage.cli.run_file_cmd import RunFileCmd
2+
from sage.cli.options import CliOptions
3+
from unittest.mock import patch
4+
import sys
5+
6+
7+
def test_run_file_cmd(capsys, tmp_path):
8+
file = tmp_path / "test.sage"
9+
file.write_text("print(3^33)")
10+
options = CliOptions(file=[str(file)])
11+
run_file_cmd = RunFileCmd(options)
12+
13+
result = run_file_cmd.run()
14+
captured = capsys.readouterr()
15+
assert captured.out == "5559060566555523\n"
16+
17+
18+
def test_run_file_cmd_with_args(capsys, tmp_path):
19+
with patch.object(sys, 'argv', ["python3", "test.sage", "1", "1"]):
20+
file = tmp_path / "test.sage"
21+
file.write_text("import sys; print(int(sys.argv[1]) + int(sys.argv[2]))")
22+
options = CliOptions(file=[str(file), "1", "1"])
23+
run_file_cmd = RunFileCmd(options)
24+
25+
result = run_file_cmd.run()
26+
captured = capsys.readouterr()
27+
assert captured.out == "2\n"

0 commit comments

Comments
 (0)