aboutsummaryrefslogtreecommitdiff
path: root/libbutl/command.mxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2020-06-30 09:19:13 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2020-06-30 09:19:13 +0200
commit16b6f3e8a6d2d6bf49d5f8785708ed18983c0667 (patch)
tree1ac919fe66ba2d11f44b53d7363b3f86a84a72ff /libbutl/command.mxx
parent288718b4977058bdcf692422173f3642b0aa4d1d (diff)
Expose command_run()'s @-substitution functionality
Also add support for different opening and closing substitution characters.
Diffstat (limited to 'libbutl/command.mxx')
-rw-r--r--libbutl/command.mxx34
1 files changed, 34 insertions, 0 deletions
diff --git a/libbutl/command.mxx b/libbutl/command.mxx
index 02c8eb8..143d406 100644
--- a/libbutl/command.mxx
+++ b/libbutl/command.mxx
@@ -85,4 +85,38 @@ LIBBUTL_MODEXPORT namespace butl
const optional<command_substitution_map>& = nullopt,
char subst = '@',
const std::function<command_callback>& = {});
+
+ // Reusable substitution utility functions.
+ //
+ // Unlike command_run(), these support different opening and closing
+ // substitution characters (e.g., <name>). Note that unmatched closing
+ // characters are treated literally and there is no support for their
+ // escaping (which would only be necessary if we needed to support variable
+ // names containing the closing character).
+
+ // Perform substitutions in a string. The second argument should be the
+ // position of the openning substitution character in the passed string.
+ // Throw invalid_argument for a malformed substitution or an unknown
+ // variable name.
+ //
+ LIBBUTL_SYMEXPORT std::string
+ command_substitute (const std::string&, std::size_t,
+ const command_substitution_map&,
+ char open, char close);
+
+ // As above but using a callback instead of a map.
+ //
+ // Specifically, on success, the callback should substitute the specified
+ // variable in out by appending its value and returning true. On failure,
+ // the callback can either throw invalid_argument or return false, in which
+ // case the standard "unknown substitution variable ..." exception will be
+ // thrown.
+ //
+ using command_substitution_callback =
+ bool (const std::string& var, std::string& out);
+
+ LIBBUTL_SYMEXPORT std::string
+ command_substitute (const std::string&, std::size_t,
+ const std::function<command_substitution_callback>&,
+ char open, char close);
}