-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3c414f
commit f6ed78e
Showing
6 changed files
with
122 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"""Helpers for the tap, target and mapper CLIs.""" | ||
|
||
from __future__ import annotations | ||
|
||
from singer_sdk.cli.options import NestedOption | ||
|
||
__all__ = ["NestedOption"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Helpers for building the CLI for a Singer tap or target.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Sequence | ||
|
||
import click | ||
|
||
|
||
class NestedOption(click.Option): | ||
"""A Click option that has suboptions.""" | ||
|
||
def __init__( | ||
self, | ||
*args: Any, | ||
suboptions: Sequence[click.Option] | None = None, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Initialize the option. | ||
Args: | ||
*args: Positional arguments to pass to the parent class. | ||
suboptions: A list of suboptions to be added to the context. | ||
**kwargs: Keyword arguments to pass to the parent class. | ||
""" | ||
self.suboptions = suboptions or [] | ||
super().__init__(*args, **kwargs) | ||
|
||
def handle_parse_result( | ||
self, | ||
ctx: click.Context, | ||
opts: dict[str, Any], | ||
args: Sequence[Any], | ||
) -> tuple[Any, list[str]]: | ||
"""Handle the parse result. | ||
Args: | ||
ctx: The Click context. | ||
opts: The options. | ||
args: The arguments. | ||
Raises: | ||
UsageError: If an option is used without the parent option. | ||
Returns: | ||
The parse result. | ||
""" | ||
ctx.ensure_object(dict) | ||
ctx.obj[self.name] = {} | ||
|
||
if self.name in opts: | ||
for option in self.suboptions: | ||
value = opts.get(option.name, option.get_default(ctx)) | ||
ctx.obj[self.name][option.name] = value | ||
else: | ||
for option in self.suboptions: | ||
if option.name in opts: | ||
errmsg = f"{option.opts[0]} is not allowed without {self.opts[0]}" | ||
raise click.UsageError(errmsg) | ||
|
||
return super().handle_parse_result(ctx, opts, args) | ||
|
||
def as_params(self) -> list[click.Option]: | ||
"""Return a list of options, including this one and its suboptions. | ||
Returns: | ||
List of options. | ||
""" | ||
return [self, *self.suboptions] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters