Skip to content
21 changes: 15 additions & 6 deletions pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ def dummy_context(arg):
yield arg


def build_arg_string(kwdict, infile=None, outfile=None):
def build_arg_string(kwdict, confdict=None, infile=None, outfile=None):
r"""
Convert a dict and optional input/output files into a GMT argument string.
Convert keyword dicts and input/output files into a GMT argument string.
Comment thread
seisman marked this conversation as resolved.
Outdated

Make sure all values in ``kwdict`` have been previously converted to a
string representation using the ``kwargs_to_strings`` decorator. The only
exceptions are True, False and None.

Any lists or tuples left will be interpreted as multiple entries for the
same command line argument. For example, the kwargs entry ``'B': ['xa',
same command line option. For example, the kwargs entry ``'B': ['xa',
'yaf']`` will be converted to ``-Bxa -Byaf`` in the argument string.

Note that spaces `` `` in arguments are converted to the equivalent octal
Expand All @@ -140,6 +140,8 @@ def build_arg_string(kwdict, infile=None, outfile=None):
----------
kwdict : dict
A dict containing parsed keyword arguments.
Comment thread
seisman marked this conversation as resolved.
Outdated
confdict : dict
A dict containing GMT's configurations.
Comment thread
seisman marked this conversation as resolved.
Outdated
infile : str or pathlib.Path
The input file.
outfile : str or pathlib.Path
Expand All @@ -149,8 +151,10 @@ def build_arg_string(kwdict, infile=None, outfile=None):
-------
args : str
The space-delimited argument string with '-' inserted before each
keyword. The arguments are sorted alphabetically, with optional input
file at the beginning and optional output file at the end.
keyword, or '--' inserted before GMT's configuration key-value pairs.
Comment thread
seisman marked this conversation as resolved.
Outdated
The keyword arguments are sorted alphabetically, followed by GMT's
Comment thread
seisman marked this conversation as resolved.
Outdated
configuration key-value pairs, with optional input file at the
beginning and optional output file at the end.

Examples
--------
Expand Down Expand Up @@ -199,11 +203,12 @@ def build_arg_string(kwdict, infile=None, outfile=None):
>>> print(
... build_arg_string(
... dict(A="0", B=True, C="rainbow"),
... confdict=dict(FORMAT_DATE_MAP="o dd"),
... infile="input.txt",
... outfile="output.txt",
... )
... )
input.txt -A0 -B -Crainbow ->output.txt
input.txt -A0 -B -Crainbow --FORMAT_DATE_MAP="o dd" ->output.txt
"""
gmt_args = []

Expand All @@ -227,6 +232,10 @@ def build_arg_string(kwdict, infile=None, outfile=None):
_value = str(kwdict[key]).replace(" ", "")
gmt_args.append(rf"-{key}{_value}")
gmt_args = sorted(gmt_args)

if confdict:
gmt_args.extend(f'--{key}="{value}"' for key, value in confdict.items())

if infile:
gmt_args = [str(infile)] + gmt_args
if outfile:
Expand Down