Skip to content

Commit 5812fd8

Browse files
made repeated keys an error
1 parent f63293f commit 5812fd8

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ def get_args():
6363
"--user-metadata",
6464
metavar="KEY=VALUE",
6565
nargs="+",
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-
"Avoid using = in keys. If multiple = signs are present, only the first one separates the key and value. "
69-
"If you repeat key names, it will replace previous values. "
70-
"If a key or value contains whitespace, enclose it in quotes. "
66+
help="Specify key-value pairs to be loaded into metadata using the format key=value. "
67+
"Separate pairs with whitespace, and ensure no whitespaces before or after the = sign. "
68+
"Avoid using = in keys. If multiple = signs are present, only the first separates the key and value. "
69+
"Please do not repeat key names. If a key or value contains whitespace, enclose it in quotes. "
7170
'For example, you can specify -u "facility=NSLS II" beamline=28ID-2 "favorite color"=blue',
7271
)
7372
args = p.parse_args()

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def test_set_wavelength_bad(inputs, msg):
8585
[["facility=NSLS II", "beamline=28ID-2", "favorite color=blue"]],
8686
[["facility", "NSLS II"], ["beamline", "28ID-2"], ["favorite color", "blue"]],
8787
),
88-
([["facility=NSLS II", "facility=NSLS III"]], [["facility", "NSLS III"]]),
8988
([["x=y=z"]], [["x", "y=z"]]),
9089
]
9190

@@ -105,16 +104,33 @@ def test_load_user_metadata(inputs, expected):
105104

106105

107106
params6 = [
108-
([["facility=NSLS", "II"]]),
109-
([["favorite", "color=blue"]]),
110-
([["beamline", "=", "28ID-2"]]),
107+
(
108+
[["facility=NSLS", "II"]],
109+
[
110+
"Please provide key-value pairs in the format key=value. "
111+
"For more information, use `labpdfproc --help.`"
112+
],
113+
),
114+
(
115+
[["favorite", "color=blue"]],
116+
"Please provide key-value pairs in the format key=value. "
117+
"For more information, use `labpdfproc --help.`",
118+
),
119+
(
120+
[["beamline", "=", "28ID-2"]],
121+
"Please provide key-value pairs in the format key=value. "
122+
"For more information, use `labpdfproc --help.`",
123+
),
124+
([["facility=NSLS II", "facility=NSLS III"]], ["Please do not specify repeated keys: facility. "]),
125+
([["wavelength=2"]], ["Please do not specify repeated keys: wavelength. "]),
111126
]
112127

113128

114-
@pytest.mark.parametrize("inputs", params6)
115-
def test_load_user_metadata_bad(inputs):
129+
@pytest.mark.parametrize("inputs, msg", params6)
130+
def test_load_user_metadata_bad(inputs, msg):
116131
actual_parser = ArgumentParser()
132+
actual_parser.add_argument("--wavelength")
117133
actual_parser.add_argument("--user-metadata")
118134
actual_args = actual_parser.parse_args(["--user-metadata", inputs[0]])
119-
with pytest.raises(ValueError):
135+
with pytest.raises(ValueError, match=re.escape(msg[0])):
120136
actual_args = load_user_metadata(actual_args)

src/diffpy/labpdfproc/tools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ def load_user_metadata(args):
9393
"For more information, use `labpdfproc --help.`"
9494
)
9595
key, value = _load_key_value_pair(item)
96+
if hasattr(args, key):
97+
raise ValueError(f"Please do not specify repeated keys: {key}. ")
9698
setattr(args, key, value)
9799
delattr(args, "user_metadata")
98100
return args

0 commit comments

Comments
 (0)