Skip to content

Commit 542f65b

Browse files
edited help message with examples provided, added docstring and more tests
1 parent 4643446 commit 542f65b

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ def get_args():
6363
"--user-metadata",
6464
metavar="KEY=VALUE",
6565
nargs="+",
66-
help="specify (multiple) key-value pairs to be loaded into metadata by using key=value. "
67-
"Do not leave whitespaces before or after = sign. "
68-
"If a key or value contains whitespace, you should define it with quotes. ",
66+
help="Specify (multiple) key-value pairs to be loaded into metadata using the format key=value. "
67+
"Please separate each pair with whitespace, and make sure no whitespaces before or after the = sign. "
68+
"Please avoid putting = in a key. If you repeat key names, it will replace previous values. "
69+
"If a key or value contains whitespace, enclose it in quotes. "
70+
'For example, you can specify -u "facility=NSLS II" beamline=28ID-2 "favorite color"=blue',
6971
)
7072
args = p.parse_args()
7173
return args

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,39 @@ def test_set_wavelength_bad(inputs, msg):
8080

8181

8282
params5 = [
83-
([[]], []),
84-
([["toast=for breakfast"]], [["toast", "for breakfast"]]),
85-
([["mylist=[1,2,3.0]"]], [["mylist", "[1,2,3.0]"]]),
86-
([["weather=rainy", "day=tuesday"]], [["weather", "rainy"], ["day", "tuesday"]]),
83+
([None], []),
84+
(
85+
[["facility=NSLS II", "beamline=28ID-2", "favorite color=blue"]],
86+
[["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]],
87+
),
8788
]
8889

8990

9091
@pytest.mark.parametrize("inputs, expected", params5)
9192
def test_load_user_metadata(inputs, expected):
92-
actual_parser = ArgumentParser()
93-
actual_parser.add_argument("-u", "--user-metadata", action="append", metavar="KEY=VALUE", nargs="+")
94-
actual_args = actual_parser.parse_args([])
9593
expected_parser = ArgumentParser()
9694
expected_args = expected_parser.parse_args([])
97-
98-
setattr(actual_args, "user_metadata", inputs[0])
99-
actual_args = load_user_metadata(actual_args)
10095
for expected_pair in expected:
10196
setattr(expected_args, expected_pair[0], expected_pair[1])
97+
98+
actual_parser = ArgumentParser()
99+
actual_parser.add_argument("--user-metadata")
100+
actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]])
101+
actual_args = load_user_metadata(actual_args)
102102
assert actual_args == expected_args
103+
104+
105+
params6 = [
106+
([["facility=NSLS", "II"]]),
107+
([["favorite", "color=blue"]]),
108+
([["beamline", "=", "28ID-2"]]),
109+
]
110+
111+
112+
@pytest.mark.parametrize("inputs", params6)
113+
def test_load_user_metadata_bad(inputs):
114+
actual_parser = ArgumentParser()
115+
actual_parser.add_argument("--user-metadata")
116+
actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]])
117+
with pytest.raises(ValueError):
118+
actual_args = load_user_metadata(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,27 @@ def _load_key_value_pair(s):
7171

7272

7373
def load_user_metadata(args):
74+
"""
75+
Load user metadata into the provided argparse Namespace, raise ValueError if in incorrect format
76+
77+
Parameters
78+
----------
79+
args argparse.Namespace
80+
the arguments from the parser
81+
82+
Returns
83+
-------
84+
the updated argparse Namespace with user metadata inserted as key-value pairs
85+
86+
"""
87+
7488
if args.user_metadata:
7589
for item in args.user_metadata:
7690
if "=" not in item:
77-
raise ValueError("please provide key-value pairs in the format key=value.")
91+
raise ValueError(
92+
"Please provide key-value pairs in the format key=value. "
93+
"For more information, use `labpdfproc --help.`"
94+
)
7895
key, value = _load_key_value_pair(item)
7996
setattr(args, key, value)
8097
delattr(args, "user_metadata")

0 commit comments

Comments
 (0)