Skip to content

Commit

Permalink
Raise an exception if the given parameter is not recognized and is lo…
Browse files Browse the repository at this point in the history
…nger than 2 characters (#1792)
  • Loading branch information
seisman authored Mar 11, 2022
1 parent 4186400 commit fc352ca
Showing 1 changed file with 12 additions and 8 deletions.
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

0 comments on commit fc352ca

Please sign in to comment.