How do I use a shellescape (| echo "stuff") macro as another macros input if there are "bad" characters like _ contained.
I will use my full example as reference.
The idea is to replace \lstinputlisting{|\string"git archive --remote=ssh://git@server/repo.git VERSION path/to/file 2>/dev/null | tar --extract --file - --to-stdout\string"}
with a macro like this
% set these so they can later be redefined
\providecommand{\GitRemote}{}
\providecommand{\GitIdentifier}{master}
\providecommand{\GitCheckout}[2][\GitIdentifier]{%
% #1 being the version/branch
% #2 being the file
| \string"git archive --remote=\GitRemote #1 \detokenize{#2} 2>/dev/null | tar --extract --file - --to-stdout \string"%
}
with usage
\lstinputlisting{\GitCheckout{path_to_file.py}}
MWE:
\documentclass{article}
\usepackage{listings}
\providecommand{\GitRemote}{}
\providecommand{\GitIdentifier}{master}
\providecommand{\GitCheckout}[2][\GitIdentifier]{%
% #1 being the version/branch
% #2 being the file
| \string"git archive --remote=\GitRemote #1 \detokenize{#2} 2>/dev/null | tar --extract --file - --to-stdout \string"%
}
\begin{document}
renewcommand{\GitRemote}{ssh://git@trac.sagemath.org/sage.git}
\lstinputlisting{\GitCheckout{src/sage/coding/goppa.py}}
\end{document}
but i keep getting errors like this
mwe.tex|10 error| Use of \\GitCheckout doesn't match its definition.
so it seems there is some escaping going on (\ to \\)
\GitCheckoutto take an optional argument which means that it does not work by expansion, and the argument tolstinputlistingmust expand to a filename. – David Carlisle Jul 26 '19 at 23:01\providecommand. Anyway, if you usexparse, then you can try\NewDocumentCommand{\GitCheckout}{O{\GitIdentifier}m}{...}– egreg Jul 26 '19 at 23:21