Skip to content

Commit 1a64b90

Browse files
authored
can now specify output filepath and output filename defaults to time
1 parent 1fadd2a commit 1a64b90

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

db_diff/cli.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import click
22
import json as std_json
33
import time as time_module
4+
import os
5+
import datetime
46
from . import load_csv, load_json, compare, human_text
57

68
@click.command(
79
context_settings={"help_option_names": ["-h", "--help"]},
810
help="Compare the differences between two CSV or JSON files to find differences.",
9-
epilog="Example: db-diff old.csv new.csv --key=Id --output=jsonfile --outputfile=diff.json",
11+
epilog="Example: db-diff old.csv new.csv --key=Id --output=jsonfile --outfilename=diff.json",
1012
)
1113
@click.argument(
1214
"previous",
@@ -29,12 +31,19 @@
2931
help="Output format. Available: (readable|json|jsonfile).",
3032
)
3133
@click.option(
32-
"--outputfile",
34+
"--outfilename",
3335
"output_file",
3436
type=click.Path(file_okay=True, dir_okay=False, writable=True, resolve_path=True),
3537
default=None,
3638
help="File to write JSON output to (only used with --output=jsonfile).",
3739
)
40+
@click.option(
41+
"--outfilepath",
42+
"output_path",
43+
type=click.Path(file_okay=False, dir_okay=True, writable=True, resolve_path=True),
44+
default=None,
45+
help="Directory to save the output file (only used with --output=jsonfile).",
46+
)
3847
@click.option(
3948
"--showunchanged",
4049
"show_unchanged",
@@ -63,19 +72,23 @@
6372
@click.version_option()
6473
def cli(
6574
previous, current, key, input_format,
66-
show_unchanged, encoding, show_time, output, output_file
75+
show_unchanged, encoding, show_time, output, output_file, output_path
6776
):
6877
"""Compare the differences between two CSV or JSON files."""
6978
dialect = {
7079
"csv": "excel",
7180
"tsv": "excel-tab",
7281
}
7382

74-
if output == "jsonfile" and not output_file:
75-
raise click.UsageError(
76-
"--outputfile must be specified when --output=jsonfile",
77-
ctx=click.get_current_context(),
78-
)
83+
# Set default output filename if needed
84+
if output == "jsonfile":
85+
if not output_file:
86+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
87+
output_file = f"{timestamp}_diffs.json"
88+
if output_path:
89+
if not os.path.isdir(output_path):
90+
raise click.ClickException(f"Output path '{output_path}' does not exist or is not a directory.")
91+
output_file = os.path.join(output_path, os.path.basename(output_file))
7992

8093
def sniff_key_column(filename, input_format):
8194
# Only for CSV/TSV/JSON

0 commit comments

Comments
 (0)