Skip to content

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

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

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,16 @@ 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 = []
# raise an exception for unrecognized options
for key in kwargs:
if len(key) > 2:
raise GMTInvalidInput(f"Unrecognized parameter '{key}'.")
# Exclude arguments that are None and False
Copy link
Member Author

@seisman seisman Mar 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can rewrite the whole function like below, so we don't need to loop over kwargs three times.

    for key in filtered_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]):
            for value in kwargs[key]:
                gmt_args.append(f"-{key}{value}")
        elif kwargs[key] is True:
            gmt_args.append(f"-{key}")
        else:
            gmt_args.append(f"-{key}{kwargs[key]}")
    return " ".join(sorted(gmt_args))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks good. It might conflict with #1487 but we can fix it later.

filtered_kwargs = {
k: v for k, v in kwargs.items() if (v is not None and v is not False)
Expand Down