|
16 | 16 | """`tfds new` command.""" |
17 | 17 |
|
18 | 18 | import argparse |
| 19 | +import dataclasses |
19 | 20 | import os |
20 | 21 | import pathlib |
21 | 22 | import subprocess |
22 | 23 | import textwrap |
23 | | -from typing import Optional |
| 24 | +import typing |
24 | 25 |
|
| 26 | +import simple_parsing |
25 | 27 | from tensorflow_datasets.core import constants |
26 | 28 | from tensorflow_datasets.core import dataset_metadata |
27 | 29 | from tensorflow_datasets.core import naming |
|
30 | 32 | from tensorflow_datasets.scripts.cli import cli_utils as utils |
31 | 33 |
|
32 | 34 |
|
33 | | -def register_subparser(parsers: argparse._SubParsersAction) -> None: # pylint: disable=protected-access |
34 | | - """Add subparser for `new` command.""" |
35 | | - new_parser = parsers.add_parser( |
36 | | - 'new', help='Creates a new dataset directory from the template.' |
37 | | - ) |
38 | | - new_parser.add_argument( |
39 | | - 'dataset_name', # Positional argument |
40 | | - type=str, |
41 | | - help='Name of the dataset to be created (in snake_case)', |
| 35 | +@dataclasses.dataclass(frozen=True, kw_only=True) |
| 36 | +class Args: |
| 37 | + """CLI arguments for creating a new dataset directory. |
| 38 | +
|
| 39 | + Attributes: |
| 40 | + dataset_name: Name of the dataset to be created (in snake_case). |
| 41 | + data_format: Format of the input data, which is used to generate a |
| 42 | + format-specific template. |
| 43 | + dir: Path where the dataset directory will be created. Defaults to current |
| 44 | + directory. |
| 45 | + """ |
| 46 | + |
| 47 | + dataset_name: str = simple_parsing.field( |
| 48 | + positional=True, |
| 49 | + # Need to explicitly set metavar for command-line help. |
| 50 | + metavar='dataset_name', |
42 | 51 | ) |
43 | | - new_parser.add_argument( |
44 | | - '--data_format', # Optional argument |
45 | | - type=str, |
| 52 | + data_format: str = simple_parsing.choice( |
| 53 | + builder_templates.STANDARD, |
| 54 | + builder_templates.CONLL, |
| 55 | + builder_templates.CONLLU, |
46 | 56 | default=builder_templates.STANDARD, |
47 | | - choices=[ |
48 | | - builder_templates.STANDARD, |
49 | | - builder_templates.CONLL, |
50 | | - builder_templates.CONLLU, |
51 | | - ], |
52 | | - help=( |
53 | | - 'Optional format of the input data, which is used to generate a ' |
54 | | - 'format-specific template.' |
55 | | - ), |
56 | | - ) |
57 | | - new_parser.add_argument( |
58 | | - '--dir', |
59 | | - type=pathlib.Path, |
60 | | - default=pathlib.Path.cwd(), |
61 | | - help=( |
62 | | - 'Path where the dataset directory will be created. ' |
63 | | - 'Defaults to current directory.' |
64 | | - ), |
65 | 57 | ) |
66 | | - new_parser.set_defaults(subparser_fn=_create_dataset_files) |
67 | | - |
68 | | - |
69 | | -def _create_dataset_files(args: argparse.Namespace) -> None: |
70 | | - """Creates the dataset directory. Executed by `tfds new <name>`.""" |
71 | | - if not naming.is_valid_dataset_and_class_name(args.dataset_name): |
72 | | - raise ValueError( |
73 | | - 'Invalid dataset name. It should be a valid Python class name.' |
| 58 | + dir: pathlib.Path = simple_parsing.field(default_factory=pathlib.Path.cwd) |
| 59 | + |
| 60 | + def execute(self) -> None: |
| 61 | + """Creates the dataset directory.""" |
| 62 | + if not naming.is_valid_dataset_and_class_name(self.dataset_name): |
| 63 | + raise ValueError( |
| 64 | + 'Invalid dataset name. It should be a valid Python class name.' |
| 65 | + ) |
| 66 | + |
| 67 | + create_dataset_files( |
| 68 | + dataset_name=self.dataset_name, |
| 69 | + dataset_dir=self.dir, |
| 70 | + data_format=self.data_format, |
74 | 71 | ) |
75 | 72 |
|
76 | | - create_dataset_files( |
77 | | - dataset_name=args.dataset_name, |
78 | | - dataset_dir=args.dir, |
79 | | - data_format=args.data_format, |
| 73 | + |
| 74 | +def register_subparser(parsers: argparse._SubParsersAction) -> None: |
| 75 | + """Add subparser for `new` command.""" |
| 76 | + parser = parsers.add_parser( |
| 77 | + 'new', |
| 78 | + help='Creates a new dataset directory from the template.', |
80 | 79 | ) |
| 80 | + parser = typing.cast(simple_parsing.ArgumentParser, parser) |
| 81 | + parser.add_arguments(Args, dest='args') |
| 82 | + parser.set_defaults(subparser_fn=lambda args: args.args.execute()) |
81 | 83 |
|
82 | 84 |
|
83 | 85 | def create_dataset_files( |
84 | 86 | dataset_name: str, |
85 | 87 | dataset_dir: pathlib.Path, |
86 | | - data_format: Optional[str] = None, |
| 88 | + data_format: str | None = None, |
87 | 89 | ) -> None: |
88 | 90 | """Creates the dataset files.""" |
89 | 91 | # Creates the root directory |
|
0 commit comments