Skip to content

Commit

Permalink
Move formatting arguments under list subcommand (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilmarty authored Jul 30, 2024
1 parent c943285 commit 209e356
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
28 changes: 16 additions & 12 deletions src/safaribookmarks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,32 @@ def _render_children(self, item: SafariBookmarkItem, format: str, depth: int = 0
def _render(
self,
root: SafariBookmarkItem,
format: Optional[str] = None,
simple_format=False,
format: str,
only_children=False,
json=False,
):
if json:
self.output.write(root.json())
elif only_children:
self._render_children(root, format=format)
else:
if simple_format:
format = SIMPLE_FORMAT
elif format is None:
format = DEFAULT_LIST_FORMAT
if only_children:
self._render_children(root, format=format)
else:
self._render_item(root, format=format)
self._render_item(root, format=format)

def list(self, path: List[str] = [], **kwargs):
def list(
self,
path: List[str] = [],
format: Optional[str] = None,
simple_format=False,
json=False,
):
target = self._get_or_walk(path)
if target is None:
raise ValueError("Target not found")
self._render(target, only_children=target.is_folder, **kwargs)
if simple_format:
format = SIMPLE_FORMAT
elif format is None:
format = DEFAULT_LIST_FORMAT
self._render(target, only_children=target.is_folder, format=format, json=json)

def add(
self,
Expand Down
28 changes: 14 additions & 14 deletions src/safaribookmarks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ def parse_args() -> Namespace:
default="~/Library/Safari/Bookmarks.plist",
help="The path to the Safari bookmarks.",
)
parser.add_argument(
"--json",
action=BooleanOptionalAction,
default=False,
help="Render output as JSON",
subparsers = parser.add_subparsers(
title="commands",
required=True,
)
parser_list = subparsers.add_parser(
"list",
aliases=["ls", "show"],
description="List bookmarks and folders.",
)
group = parser.add_mutually_exclusive_group(required=False)
group = parser_list.add_mutually_exclusive_group(required=False)
group.add_argument(
"--format",
"-F",
Expand All @@ -45,14 +48,11 @@ def parse_args() -> Namespace:
default=False,
help="Set the output to a simple format. Not to be used with --format.",
)
subparsers = parser.add_subparsers(
title="commands",
required=True,
)
parser_list = subparsers.add_parser(
"list",
aliases=["ls", "show"],
description="List bookmarks and folders.",
group.add_argument(
"--json",
action=BooleanOptionalAction,
default=False,
help="Render output as JSON",
)
parser_list.add_argument(
"path",
Expand Down

0 comments on commit 209e356

Please sign in to comment.