Skip to content

Commit 7cf3723

Browse files
committed
Update argparse usage to support 3.14
Our usage of store_const with positionals is now unsupported and gives a ValueError on 3.14, this change maintains the same behaviour (splitting based on --- to fill nsjail_args and py_args)
1 parent a0bf859 commit 7cf3723

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

snekbox/__main__.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,40 @@ def parse_args() -> argparse.Namespace:
1414
parser.add_argument("code", help="the Python code to evaluate")
1515
parser.add_argument(
1616
"nsjail_args",
17-
action="store_const",
18-
const=[],
19-
help="override configured NsJail options (default: [])",
17+
nargs="*",
18+
default=[],
19+
help="override configured NsJail options",
2020
)
2121
parser.add_argument(
2222
"py_args",
23-
action="store_const",
24-
const=["-c"],
25-
help="arguments to pass to the Python process (default: ['-c'])",
23+
nargs="*",
24+
default=["-c"],
25+
help="arguments to pass to the Python process",
2626
)
27-
2827
# nsjail_args and py_args are just dummies for documentation purposes.
2928
# Their actual values come from all the unknown arguments.
3029
# There doesn't seem to be a better solution with argparse.
31-
args, unknown = parser.parse_known_args()
32-
try:
33-
# Can't use double dash because that has special semantics for argparse already.
34-
split = unknown.index("---")
35-
args.nsjail_args = unknown[:split]
36-
args.py_args = unknown[split + 1 :]
37-
except ValueError:
38-
args.nsjail_args = unknown
30+
31+
# Split sys.argv based on '---' separator
32+
if "---" in sys.argv:
33+
separator_index = sys.argv.index("---")
34+
args_before = sys.argv[1:separator_index]
35+
args_after = sys.argv[separator_index + 1 :]
36+
else:
37+
args_before = sys.argv[1:]
38+
args_after = None
39+
40+
# Parse only the first positional argument (code) and known flags
41+
args, unknown = parser.parse_known_args(args_before[:1] if args_before else [])
42+
43+
# Everything after 'code' and before '---' goes to nsjail_args
44+
args.nsjail_args = args_before[1:] if len(args_before) > 1 else []
45+
46+
# Everything after '---' goes to py_args
47+
if args_after is not None:
48+
args.py_args = args_after
49+
else:
50+
args.py_args = ["-c"]
3951

4052
return args
4153

0 commit comments

Comments
 (0)