Skip to content

Commit 4d97384

Browse files
committed
Update: delay writing to the file.
1 parent 8e9a260 commit 4d97384

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

cmdcomp/app.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import sys
44
from argparse import BooleanOptionalAction, FileType
5+
from pathlib import Path
56
from typing import NoReturn
67

78
from rich.console import Console as RichConsole
@@ -62,7 +63,7 @@ def run(cls, args: list[str] | None = None) -> None:
6263
parser.add_argument(
6364
"--output-file",
6465
"-o",
65-
type=FileType("w"),
66+
type=Path,
6667
help="output file path. default is [lit]stdout[/].",
6768
)
6869

@@ -93,14 +94,19 @@ def run(cls, args: list[str] | None = None) -> None:
9394
try:
9495
from cmdcomp import completion, config
9596

96-
print(
97-
completion.generate(
98-
space.shell_type,
99-
config.load(space.file),
100-
),
101-
file=space.output_file,
97+
output_file: Path | None = space.output_file
98+
99+
output = completion.generate(
100+
space.shell_type,
101+
config.load(space.file),
102102
)
103103

104+
if output_file:
105+
with open(output_file, "w") as f:
106+
f.write(output)
107+
else:
108+
print(output)
109+
104110
except Exception as e:
105111
if space.verbose:
106112
logger.exception(e)

0 commit comments

Comments
 (0)