Skip to content

Commit b7210ff

Browse files
committed
Deal with boolean values in build_arg_string
1 parent 06bee27 commit b7210ff

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

pygmt/helpers/utils.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def build_arg_string(kwargs):
106106
Transform keyword arguments into a GMT argument string.
107107
108108
Make sure all arguments have been previously converted to a string
109-
representation using the ``kwargs_to_strings`` decorator.
109+
representation using the ``kwargs_to_strings`` decorator. The only
110+
exceptions are True, False and None.
110111
111112
Any lists or tuples left will be interpreted as multiple entries for the
112113
same command line argument. For example, the kwargs entry ``'B': ['xa',
@@ -128,10 +129,20 @@ def build_arg_string(kwargs):
128129
129130
>>> print(
130131
... build_arg_string(
131-
... dict(R="1/2/3/4", J="X4i", P="", E=200, X=None, Y=None)
132+
... dict(
133+
... R="1/2/3/4",
134+
... J="X4i",
135+
... P="",
136+
... E=200,
137+
... X=None,
138+
... Y=None,
139+
... A=True,
140+
... B=False,
141+
... Z=0,
142+
... )
132143
... )
133144
... )
134-
-E200 -JX4i -P -R1/2/3/4
145+
-A -E200 -JX4i -P -R1/2/3/4 -Z0
135146
>>> print(
136147
... build_arg_string(
137148
... dict(
@@ -144,18 +155,19 @@ def build_arg_string(kwargs):
144155
... )
145156
-Bxaf -Byaf -BWSen -I1/1p,blue -I2/0.25p,blue -JX4i -R1/2/3/4
146157
"""
158+
147159
sorted_args = []
148160
for key in sorted(kwargs):
149161
if is_nonstr_iter(kwargs[key]):
150162
for value in kwargs[key]:
151-
sorted_args.append("-{}{}".format(key, value))
152-
elif kwargs[key] is None: # arguments like -XNone are invalid
163+
sorted_args.append(f"-{key}{value}")
164+
elif kwargs[key] is None or kwargs[key] is False: # Skip None and False
153165
continue
166+
elif kwargs[key] is True:
167+
sorted_args.append(f"-{key}")
154168
else:
155-
sorted_args.append("-{}{}".format(key, kwargs[key]))
156-
157-
arg_str = " ".join(sorted_args)
158-
return arg_str
169+
sorted_args.append(f"-{key}{kwargs[key]}")
170+
return " ".join(sorted_args)
159171

160172

161173
def is_nonstr_iter(value):

0 commit comments

Comments
 (0)