Skip to content

Commit 87628f9

Browse files
save intermediate progress, can ignore
1 parent 8a5dafa commit 87628f9

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ def get_args():
6060
p.add_argument(
6161
"-u",
6262
"--user-metadata",
63-
metavar=("KEY=VALUE"),
64-
action="append",
65-
help="specify key-value pairs to be loaded into metadata by using key=value. "
66-
"You can specify multiple paris by calling -u multiple times.",
63+
metavar="KEY=VALUE",
64+
nargs="+",
65+
help="specify (multiple) key-value pairs to be loaded into metadata by using key=value. "
66+
"Do not leave whitespaces before or after = sign. "
67+
"If a key or value contains whitespace, you should define it with quotes. ",
6768
)
6869
args = p.parse_args()
6970
return args

src/diffpy/labpdfproc/tests/test_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414

1515
@pytest.mark.parametrize("inputs, expected", params5)
16-
def test_load_additional_info(inputs, expected):
16+
def test_load_user_metadata(inputs, expected):
1717
actual_parser = ArgumentParser()
18-
actual_parser.add_argument("-u", "--user-metadata", action="append", metavar=("KEY=VALUE"))
18+
actual_parser.add_argument("-u", "--user-metadata", action="append", metavar="KEY=VALUE", nargs="+")
1919
actual_args = actual_parser.parse_args([])
2020
expected_parser = ArgumentParser()
2121
expected_args = expected_parser.parse_args([])

src/diffpy/labpdfproc/tools.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import sys
1+
def load_key_value_pair(s):
2+
items = s.split("=")
3+
key = items[0].strip()
4+
if len(items) > 1:
5+
value = "=".join(items[1:])
6+
return (key, value)
27

38

49
def load_user_metadata(args):
510
if args.user_metadata:
611
for item in args.user_metadata:
7-
if not item:
8-
sys.exit(
9-
"please provide at least one key-value pair in the format key=value. "
10-
"you can exclude -add if you don't want to provide additional info."
11-
)
1212
if "=" not in item:
13-
sys.exit("please provide key-value pairs in the format key=value.")
14-
key, value = item.split("=", 1)
15-
# if "=" in value:
16-
# sys.exit("please use only one equals sign for key=value.")
13+
raise ValueError("please provide key-value pairs in the format key=value.")
14+
key, value = load_key_value_pair(item)
1715
setattr(args, key, value)
1816
delattr(args, "user_metadata")
1917
return args
18+
19+
20+
# Notes, can ignore
21+
# ideal inputs: "key1=value1" "key2=value2"
22+
# (different pairs are separated by white spaces, key and value are separated by =)
23+
24+
# question 1: white spaces in key, value, both?
25+
# if value contains whitespace, you can specify key="value value" or "key=value value"
26+
# if key contains whitespace, for example, you have "key key=value",
27+
# then you can access like getattr(args, 'key key'), but we dont recommend this
28+
29+
# question 2: more than one =?
30+
# if i have key is hello, value is hello=world, then i can have hello=hello=world to process okay
31+
# if i have = in key then it's still processing as value => would this be an issue?

0 commit comments

Comments
 (0)