Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an exception if the given parameter is not recognized and is longer than 2 characters #1792

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,20 @@ def build_arg_string(kwargs):
... )
... )
-BWSen -Bxaf -Byaf -I1/1p,blue -I2/0.25p,blue -JX4i -R1/2/3/4
>>> print(build_arg_string(dict(R="1/2/3/4", J="X4i", watre=True)))
Traceback (most recent call last):
...
pygmt.exceptions.GMTInvalidInput: Unrecognized parameter 'watre'.
"""
gmt_args = []
# Exclude arguments that are None and False
filtered_kwargs = {
k: v for k, v in kwargs.items() if (v is not None and v is not False)
}
for key in filtered_kwargs:
if is_nonstr_iter(kwargs[key]):
for value in kwargs[key]:
gmt_args.append(f"-{key}{value}")

for key in kwargs:
if len(key) > 2: # raise an exception for unrecognized options
raise GMTInvalidInput(f"Unrecognized parameter '{key}'.")
if kwargs[key] is None or kwargs[key] is False:
pass # Exclude arguments that are None and False
elif is_nonstr_iter(kwargs[key]):
gmt_args.extend(f"-{key}{value}" for value in kwargs[key])
elif kwargs[key] is True:
gmt_args.append(f"-{key}")
else:
Expand Down