@@ -119,12 +119,12 @@ def dummy_context(arg):
119
119
yield arg
120
120
121
121
122
- def build_arg_string (kwargs ):
122
+ def build_arg_string (kwdict , infile = None , outfile = None ):
123
123
r"""
124
- Transform keyword arguments into a GMT argument string.
124
+ Convert a dict and optional input/output files into a GMT argument string.
125
125
126
- Make sure all arguments have been previously converted to a string
127
- representation using the ``kwargs_to_strings`` decorator. The only
126
+ Make sure all values in ``kwdict`` have been previously converted to a
127
+ string representation using the ``kwargs_to_strings`` decorator. The only
128
128
exceptions are True, False and None.
129
129
130
130
Any lists or tuples left will be interpreted as multiple entries for the
@@ -138,14 +138,19 @@ def build_arg_string(kwargs):
138
138
139
139
Parameters
140
140
----------
141
- kwargs : dict
142
- Parsed keyword arguments.
141
+ kwdict : dict
142
+ A dict containing parsed keyword arguments.
143
+ infile : str or pathlib.Path
144
+ The input file.
145
+ outfile : str or pathlib.Path
146
+ The output file.
143
147
144
148
Returns
145
149
-------
146
150
args : str
147
151
The space-delimited argument string with '-' inserted before each
148
- keyword. The arguments are sorted alphabetically.
152
+ keyword. The arguments are sorted alphabetically, with optional input
153
+ file at the begining and optioanl output file at the end.
149
154
150
155
Examples
151
156
--------
@@ -191,29 +196,42 @@ def build_arg_string(kwargs):
191
196
... )
192
197
... )
193
198
-BWSne+tBlank\040Space -Baf -F+t"Empty\040\040Spaces" -l'Void\040Space'
199
+ >>> print(
200
+ ... build_arg_string(
201
+ ... dict(A="0", B=True, C="rainbow"),
202
+ ... infile="input.txt",
203
+ ... outfile="output.txt",
204
+ ... )
205
+ ... )
206
+ input.txt -A0 -B -Crainbow ->output.txt
194
207
"""
195
208
gmt_args = []
196
209
197
- for key in kwargs :
210
+ for key in kwdict :
198
211
if len (key ) > 2 : # raise an exception for unrecognized options
199
212
raise GMTInvalidInput (f"Unrecognized parameter '{ key } '." )
200
- if kwargs [key ] is None or kwargs [key ] is False :
213
+ if kwdict [key ] is None or kwdict [key ] is False :
201
214
pass # Exclude arguments that are None and False
202
- elif is_nonstr_iter (kwargs [key ]):
203
- for value in kwargs [key ]:
215
+ elif is_nonstr_iter (kwdict [key ]):
216
+ for value in kwdict [key ]:
204
217
_value = str (value ).replace (" " , r"\040" )
205
218
gmt_args .append (rf"-{ key } { _value } " )
206
- elif kwargs [key ] is True :
219
+ elif kwdict [key ] is True :
207
220
gmt_args .append (f"-{ key } " )
208
221
else :
209
222
if key != "J" : # non-projection parameters
210
- _value = str (kwargs [key ]).replace (" " , r"\040" )
223
+ _value = str (kwdict [key ]).replace (" " , r"\040" )
211
224
else :
212
225
# special handling if key == "J" (projection)
213
226
# remove any spaces in PROJ4 string
214
- _value = str (kwargs [key ]).replace (" " , "" )
227
+ _value = str (kwdict [key ]).replace (" " , "" )
215
228
gmt_args .append (rf"-{ key } { _value } " )
216
- return " " .join (sorted (gmt_args ))
229
+ gmt_args = sorted (gmt_args )
230
+ if infile :
231
+ gmt_args = [str (infile )] + gmt_args
232
+ if outfile :
233
+ gmt_args .append ("->" + str (outfile ))
234
+ return " " .join (gmt_args )
217
235
218
236
219
237
def is_nonstr_iter (value ):
0 commit comments