Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eba6f5b

Browse files
committed
CLI exits with code 1 in case of failure
1 parent 9a5bded commit eba6f5b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/pdl/pdl.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def main():
205205
# This case must be before `if args.pdl is None:`
206206
if args.version:
207207
print(f"PDL {version}")
208-
return
208+
return 0
209209

210210
# This case must be before `if args.pdl is None:`
211211
if args.schema:
@@ -218,11 +218,11 @@ def main():
218218
)
219219
top_level_schema["anyOf"] = list(schema.values())
220220
print(json.dumps(top_level_schema, indent=2))
221-
return
221+
return 0
222222

223223
if args.pdl is None:
224224
parser.print_help()
225-
return
225+
return 0
226226

227227
if args.sandbox:
228228
args = sys.argv[1:]
@@ -267,12 +267,13 @@ def main():
267267
batch=batch,
268268
cwd=pdl_file.parent,
269269
)
270-
pdl_interpreter.generate(
270+
exit_code = pdl_interpreter.generate(
271271
pdl_file,
272272
InterpreterState(**config),
273273
PdlDict(initial_scope),
274274
trace_file,
275275
)
276+
return exit_code
276277

277278

278279
if __name__ == "__main__":

src/pdl/pdl_interpreter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,17 @@ def generate(
149149
state: Optional[InterpreterState],
150150
initial_scope: ScopeType,
151151
trace_file: Optional[str | Path],
152-
):
152+
) -> int:
153153
"""Execute the PDL program defined in `pdl_file`.
154154
155155
Args:
156156
pdl_file: Program to execute.
157157
initial_scope: Environment defining the variables in scope to execute the program.
158158
state: Initial state of the interpreter.
159159
trace_file: Indicate if the execution trace must be produced and the file to save it.
160+
161+
Returns:
162+
Returns the exit code: `0` for success, `1` for failure
160163
"""
161164
try:
162165
prog, loc = parse_file(pdl_file)
@@ -172,6 +175,7 @@ def generate(
172175
write_trace(trace_file, trace)
173176
except PDLParseError as exc:
174177
print("\n".join(exc.message), file=sys.stderr)
178+
return 1
175179
except PDLRuntimeError as exc:
176180
if exc.loc is None:
177181
message = exc.message
@@ -180,7 +184,8 @@ def generate(
180184
print(message, file=sys.stderr)
181185
if trace_file and exc.pdl__trace is not None:
182186
write_trace(trace_file, exc.pdl__trace)
183-
raise exc
187+
return 1
188+
return 0
184189

185190

186191
def write_trace(

0 commit comments

Comments
 (0)