Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add short alias for --show-{csv,json,md} #6711

Merged
merged 1 commit into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dvc/command/data_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def add_parser(subparsers, _parent_parser):
help="Show status of all stages in the specified directory.",
)
status_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
Expand Down
11 changes: 7 additions & 4 deletions dvc/command/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def _digest(checksum):
return "{}..{}".format(checksum["old"][0:8], checksum["new"][0:8])


def _show_md(diff, show_hash=False, hide_missing=False):
def _show_markdown(diff, show_hash=False, hide_missing=False):
headers = ["Status", "Hash", "Path"] if show_hash else ["Status", "Path"]
rows = []
statuses = ["added", "deleted", "renamed", "modified"]
Expand Down Expand Up @@ -142,10 +142,10 @@ def run(self):
del entry["hash"]
diff[key] = entries

if self.args.show_json:
if self.args.json:
ui.write(json.dumps(diff))
elif self.args.show_md:
_show_md(diff, show_hash, hide_missing)
elif self.args.markdown:
_show_markdown(diff, show_hash, hide_missing)
elif diff:
self._show_diff(diff, hide_missing)

Expand Down Expand Up @@ -188,6 +188,7 @@ def add_parser(subparsers, parent_parser):
nargs="?",
)
diff_parser.add_argument(
"--json",
"--show-json",
help="Format the output into a JSON",
action="store_true",
Expand All @@ -200,9 +201,11 @@ def add_parser(subparsers, parent_parser):
default=False,
)
diff_parser.add_argument(
"--md",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I always try to use -md (single dash) on these abbreviations, so I am not a fan of this, but --markdown is too long. And --md is what we use in CML as well.

"--show-md",
help="Show tabulated output in the Markdown format (GFM).",
action="store_true",
dest="markdown",
default=False,
)
diff_parser.add_argument(
Expand Down
25 changes: 15 additions & 10 deletions dvc/command/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def baseline_styler(typ):


def show_experiments(
all_experiments, pager=True, no_timestamp=False, show_csv=False, **kwargs
all_experiments, pager=True, no_timestamp=False, csv=False, **kwargs
):
from funcy.seqs import flatten as flatten_list

Expand Down Expand Up @@ -439,7 +439,7 @@ def show_experiments(

row_styles = lmap(baseline_styler, td.column("typ"))

if not show_csv:
if not csv:
merge_headers = ["Experiment", "rev", "typ", "parent"]
td.column("Experiment")[:] = map(
prepare_exp_id, td.as_dict(merge_headers)
Expand Down Expand Up @@ -473,7 +473,7 @@ def show_experiments(
rich_table=True,
header_styles=styles,
row_styles=row_styles,
show_csv=show_csv,
csv=csv,
)


Expand Down Expand Up @@ -506,18 +506,18 @@ def run(self):
logger.exception("failed to show experiments")
return 1

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(all_experiments, default=_format_json))
else:
precision = (
self.args.precision or None
if self.args.show_csv
if self.args.csv
else DEFAULT_PRECISION
)
fill_value = "" if self.args.show_csv else FILL_VALUE
iso = True if self.args.show_csv else False
fill_value = "" if self.args.csv else FILL_VALUE
iso = True if self.args.csv else False

show_experiments(
all_experiments,
Expand All @@ -532,7 +532,7 @@ def run(self):
fill_value=fill_value,
iso=iso,
pager=not self.args.no_pager,
show_csv=self.args.show_csv,
csv=self.args.csv,
)
return 0

Expand Down Expand Up @@ -561,7 +561,7 @@ def run(self):
logger.exception("failed to show experiments diff")
return 1

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(diff))
Expand All @@ -579,7 +579,7 @@ def run(self):
show_diff(
diff[key],
title=title,
markdown=self.args.show_md,
markdown=self.args.markdown,
no_path=self.args.no_path,
old=self.args.old,
on_empty_diff="diff not supported",
Expand Down Expand Up @@ -1085,12 +1085,14 @@ def add_parser(subparsers, parent_parser):
help="Always show git commit SHAs instead of branch/tag names.",
)
experiments_show_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help="Print output in JSON format instead of a human-readable table.",
)
experiments_show_parser.add_argument(
"--csv",
"--show-csv",
action="store_true",
default=False,
Expand Down Expand Up @@ -1159,15 +1161,18 @@ def add_parser(subparsers, parent_parser):
help="Show only params that are stage dependencies.",
)
experiments_diff_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help="Show output in JSON format.",
)
experiments_diff_parser.add_argument(
"--md",
"--show-md",
action="store_true",
default=False,
dest="markdown",
help="Show tabulated output in the Markdown format (GFM).",
)
experiments_diff_parser.add_argument(
Expand Down
7 changes: 5 additions & 2 deletions dvc/command/ls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def run(self):
recursive=self.args.recursive,
dvc_only=self.args.dvc_only,
)
if self.args.show_json:
if self.args.json:
ui.write_json(entries)
elif entries:
entries = _prettify(entries, with_color=True)
Expand Down Expand Up @@ -69,7 +69,10 @@ def add_parser(subparsers, parent_parser):
"--dvc-only", action="store_true", help="Show only DVC outputs."
)
list_parser.add_argument(
"--show-json", action="store_true", help="Show output in JSON format."
"--json",
"--show-json",
action="store_true",
help="Show output in JSON format.",
)
list_parser.add_argument(
"--rev",
Expand Down
14 changes: 10 additions & 4 deletions dvc/command/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(self):
logger.exception("")
return 1

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(metrics, default=encode_exception))
Expand All @@ -40,7 +40,7 @@ def run(self):

show_metrics(
metrics,
markdown=self.args.show_md,
markdown=self.args.markdown,
all_branches=self.args.all_branches,
all_tags=self.args.all_tags,
all_commits=self.args.all_commits,
Expand All @@ -65,7 +65,7 @@ def run(self):
logger.exception("failed to show metrics diff")
return 1

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(diff))
Expand All @@ -75,7 +75,7 @@ def run(self):
show_diff(
diff,
title="Metric",
markdown=self.args.show_md,
markdown=self.args.markdown,
no_path=self.args.no_path,
precision=self.args.precision or DEFAULT_PRECISION,
round_digits=True,
Expand Down Expand Up @@ -140,15 +140,18 @@ def add_parser(subparsers, parent_parser):
help="Show metrics for all commits.",
)
metrics_show_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help="Show output in JSON format.",
)
metrics_show_parser.add_argument(
"--md",
"--show-md",
action="store_true",
default=False,
dest="markdown",
help="Show tabulated output in the Markdown format (GFM).",
)
metrics_show_parser.add_argument(
Expand Down Expand Up @@ -220,15 +223,18 @@ def add_parser(subparsers, parent_parser):
help="Show unchanged metrics as well.",
)
metrics_diff_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help="Show output in JSON format.",
)
metrics_diff_parser.add_argument(
"--md",
"--show-md",
action="store_true",
default=False,
dest="markdown",
help="Show tabulated output in the Markdown format (GFM).",
)
metrics_diff_parser.add_argument(
Expand Down
7 changes: 5 additions & 2 deletions dvc/command/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def run(self):
logger.exception("failed to show params diff")
return 1

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(diff))
Expand All @@ -35,7 +35,7 @@ def run(self):
show_diff(
diff,
title="Param",
markdown=self.args.show_md,
markdown=self.args.markdown,
no_path=self.args.no_path,
show_changes=False,
)
Expand Down Expand Up @@ -103,15 +103,18 @@ def add_parser(subparsers, parent_parser):
help="Show only params that are stage dependencies.",
)
params_diff_parser.add_argument(
"--json",
"--show-json",
action="store_true",
default=False,
help="Show output in JSON format.",
)
params_diff_parser.add_argument(
"--md",
"--show-md",
action="store_true",
default=False,
dest="markdown",
help="Show tabulated output in the Markdown format (GFM).",
)
params_diff_parser.add_argument(
Expand Down
2 changes: 1 addition & 1 deletion dvc/command/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def run(self):
if self.args.quiet:
return bool(st)

if self.args.show_json:
if self.args.json:
import json

ui.write(json.dumps(st))
Expand Down
2 changes: 1 addition & 1 deletion dvc/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def row_from_dict(self, d: Mapping[str, "CellT"]) -> None:
def render(self, **kwargs: Any):
from dvc.ui import ui

if kwargs.pop("show_csv", False):
if kwargs.pop("csv", False):
ui.write(self.to_csv(), end="")
else:
ui.table(self, headers=self.keys(), **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tests/func/experiments/test_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def _get_rev_isotimestamp(rev):
ref_info2 = first(exp_refs_by_rev(scm, rev2))

capsys.readouterr()
assert main(["exp", "show", "--show-csv"]) == 0
assert main(["exp", "show", "--csv"]) == 0
cap = capsys.readouterr()
assert (
"Experiment,rev,typ,Created,parent,metrics.yaml:foo,params.yaml:foo"
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/command/ls/test_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_list_outputs_only(mocker):


def test_show_json(mocker, capsys):
cli_args = parse_args(["list", "local_dir", "--show-json"])
cli_args = parse_args(["list", "local_dir", "--json"])
assert cli_args.func == CmdList

cmd = cli_args.func(cli_args)
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/command/test_compat_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from itertools import takewhile

import pytest

from dvc.cli import parse_args


def _id_gen(val) -> str:
if isinstance(val, list):
return "-".join(takewhile(lambda v: not v.startswith("-"), val))
return str(val)


@pytest.mark.parametrize(
"args, key",
[
(["diff", "--show-json"], "json"),
(["diff", "--show-md"], "markdown"),
(["experiments", "diff", "--show-json"], "json"),
(["experiments", "diff", "--show-md"], "markdown"),
(["experiments", "show", "--show-json"], "json"),
(["experiments", "show", "--show-csv"], "csv"),
(["ls", "--show-json", "."], "json"),
(["metrics", "diff", "--show-json"], "json"),
(["metrics", "diff", "--show-md"], "markdown"),
(["metrics", "show", "--show-json"], "json"),
(["metrics", "show", "--show-md"], "markdown"),
(["params", "diff", "--show-json"], "json"),
(["params", "diff", "--show-md"], "markdown"),
(["status", "--show-json"], "json"),
],
ids=_id_gen,
)
def test_backward_compat_flags(args, key):
"""Test support for --show-csv/--show-json/--show-md flags."""
cli_args = parse_args(args)
d = vars(cli_args)
assert d[key] is True
Loading