Skip to content

Commit 8a998e9

Browse files
committed
added more comprehensive code documentation to cli
1 parent 9fcd41a commit 8a998e9

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

coderdata/cli.py

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
1+
"""
2+
Command Line Interface to retrieve coderdata datasets.
3+
"""
4+
15
import argparse
26
from os import PathLike
37
from pathlib import Path
48
from typing import Union
59
import sys
10+
611
from .download.downloader import download as download_datasets
712
from .utils import version
813
from .utils import list_datasets
914

1015
def main():
11-
parser = argparse.ArgumentParser(prog='coderdata')
12-
subparsers = parser.add_subparsers(dest='command')
16+
"""
17+
Main method containing the argument parsing and execution of
18+
individual subroutines.
19+
"""
20+
parser = argparse.ArgumentParser(prog='coderdata', add_help=True)
21+
parser.set_defaults(func=info)
1322

14-
# Subcommand 'download'
15-
parser_download = subparsers.add_parser('download', help='Download datasets')
23+
# subparser for the 'download' subroutine
24+
subparsers = parser.add_subparsers(
25+
title="commands",
26+
dest='command'
27+
)
28+
parser_download = subparsers.add_parser(
29+
'download',
30+
help='subroutine to download datasets. See "coderdata download -h" '
31+
'for more options.'
32+
)
1633
parser_download.add_argument(
1734
'-n', '--name',
1835
dest='DATASET_NAME',
1936
type=str,
2037
default='all',
21-
help='Name of the dataset to download (e.g., "beataml"). '
22-
'Alternatively "all" will download the full repository of '
38+
help='name of the dataset to download (e.g., "beataml"). '
39+
'Alternatively, "all" will download the full repository of '
2340
'coderdata datasets. See "coderdata --list" for a complete list '
2441
'of available datasets. Defaults to "all"'
2542
)
@@ -28,40 +45,63 @@ def main():
2845
dest="LOCAL_PATH",
2946
type=check_folder,
3047
default=Path.cwd(),
31-
help='Defines the folder the datasets should be stored in. Defaults '
48+
help='defines the folder the datasets should be stored in. Defaults '
3249
'to the current working directory if omitted.'
3350
)
3451
parser_download.add_argument(
3552
'-o', '--overwrite',
3653
dest="OVERWRITE",
3754
default=False,
3855
action='store_true',
39-
help='Allow dataset files to be overwritten if they already exist.'
56+
help='allow dataset files to be overwritten if they already exist.'
4057
)
4158
parser_download.set_defaults(func=download)
4259

60+
# argument group that contains flags for additional information
4361
grp = parser.add_mutually_exclusive_group()
4462
grp.add_argument(
4563
'-l', '--list',
4664
dest="LIST",
4765
action='store_true',
48-
help="Prints list of available datasets and exits program."
66+
help="prints list of available datasets and exits program."
4967
)
5068
grp.add_argument(
5169
'-v', '--version',
5270
dest="VERSION",
5371
action='store_true',
54-
help="Prints the versions of the coderdata API and dataset and exits the program"
72+
help='prints the versions of the coderdata API and dataset and exits '
73+
'the program'
5574
)
56-
parser.set_defaults(func=info)
5775

76+
# checks if 'coderdata' was executed without additional arguments
77+
# and if so prints help message and exits
5878
if len(sys.argv) == 1:
5979
parser.print_help(sys.stderr)
6080
sys.exit(0)
81+
82+
# parse arguments and execute defined functions by `set_default()`
83+
# according to which subcommands / arguments where passed on the
84+
# command line
6185
args = parser.parse_args()
6286
args.func(args)
6387

88+
6489
def info(args):
90+
"""
91+
Helper function that takes the parsed command line arguments and
92+
prints either verison information or information on the available
93+
datasets depending on the arguments in ``args``.
94+
95+
Parameters
96+
----------
97+
args : Namespace
98+
A Namespace object that contains commandline arguments parsed by
99+
``ArgumentParser.parse_args()``.
100+
"""
101+
102+
# retrieve the dataset information stored in dataset.yml via
103+
# coderdata.utils.list_dataset() and print the information to
104+
# sys.stdout
65105
if args.LIST:
66106
print(
67107
'\n'
@@ -75,6 +115,9 @@ def info(args):
75115
'To download individual datasets run "coderdata download -name '
76116
'DATASET_NAME" where "DATASET_NAME" is for example "beataml".'
77117
)
118+
119+
# retrieve version number information stored in dataset.yml via
120+
# coderdata.utils.version() and print the information to sys.stdout
78121
elif args.VERSION:
79122
version_numbers = version()
80123
print(
@@ -88,6 +131,17 @@ def info(args):
88131

89132

90133
def download(args):
134+
"""
135+
Wrapper function to download datasets via ``coderdata.download()``.
136+
Function passes commandline arguments to the internal download
137+
function.
138+
139+
Parameters
140+
----------
141+
args : Namespace
142+
A Namespace object that contains commandline arguments parsed by
143+
``ArgumentParser.parse_args()``.
144+
"""
91145
download_datasets(
92146
name=args.DATASET_NAME,
93147
local_path=args.LOCAL_PATH,
@@ -97,7 +151,21 @@ def download(args):
97151

98152
def check_folder(path: Union[str, PathLike, Path]) -> Path:
99153
"""
100-
Helper function to check if a defined folder exists
154+
Helper function to check if a defined folder exists.
155+
156+
Returns
157+
-------
158+
Path
159+
Cleaned path object with the absolute path to the folder passed
160+
to the function.
161+
162+
Raises
163+
------
164+
TypeError
165+
If passed path argument is not of the requested type.
166+
OSError
167+
If the passed path argument does not link to a valid existing
168+
folder.
101169
"""
102170

103171
if not isinstance(path, (str, PathLike, Path)):
@@ -119,6 +187,7 @@ def check_folder(path: Union[str, PathLike, Path]) -> Path:
119187
return abs_path
120188

121189

190+
# Routine to execute the main function.
122191
if __name__ == '__main__':
123192
try:
124193
main()

0 commit comments

Comments
 (0)