<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Chewing the Cud (Posts about emacs erlang escript org-mode orgmode org-babel)</title><link>https://jamesaimonetti.com/</link><description></description><atom:link href="https://jamesaimonetti.com/categories/emacs-erlang-escript-org-mode-orgmode-org-babel.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Wed, 26 Mar 2025 20:45:28 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Org-Babel and Erlang Escripts</title><link>https://jamesaimonetti.com/posts/org-babel-and-erlang-escripts/</link><dc:creator>James Aimonetti</dc:creator><description>&lt;p&gt;
I recently wrote an &lt;a href="http://erlang.org/doc/man/escript.html"&gt;escript&lt;/a&gt; for work that used the wonderful &lt;a href="https://github.com/jcomellas/getopt"&gt;getopt&lt;/a&gt; application for processing command-line arguments. Part of that script includes printing the script usage if passed the `-h` or `–help` flag.
&lt;/p&gt;

&lt;p&gt;
As part of my initiative to use &lt;a href="http://orgmode.org/worg/org-contrib/babel/intro.html#literate-programming"&gt;orgmode's literate programming&lt;/a&gt; capabilities, I wrote a `README.org` for the script that described how to install pre-requisites, what the script does, etc. I also wanted to include the usage text as part of the doc, since part of what I've been doing was adding more flags to tune the operations of the script.
&lt;/p&gt;

&lt;p&gt;
The tricky thing is that the real usage text and what was in the doc frequently grows apart as development occurs. Wouldn't it be great to have the README.org stay in sync with what the current usage flags are?
&lt;/p&gt;

&lt;div id="outline-container-org84d360e" class="outline-2"&gt;
&lt;h2 id="org84d360e"&gt;Org-babel SRC blocks to the rescue!&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org84d360e"&gt;
&lt;p&gt;
With the appropriate SRC block, you can capture the results as part of the doc when exporting (or just capture the results in place) to a new format.
&lt;/p&gt;

&lt;p&gt;
For my use case, I used a simple shell SRC block (with appropriate headers) to run the script with the appropriate flag:
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;./my_escript&lt;span class="w"&gt; &lt;/span&gt;-h
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;
Evaluating the SRC block with `C-cC-c` creates the attending `#+RESULTS:` section below. Except…it was empty. I couldn't figure out why the script's output wasn't showing up. Simple shell commands like `pwd` showed up just fine. What do?
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-org1f74d6e" class="outline-2"&gt;
&lt;h2 id="org1f74d6e"&gt;STDERR vs STDOUT&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-org1f74d6e"&gt;
&lt;p&gt;
Ah yes; it turns out that, by default, getopt &lt;a href="https://github.com/jcomellas/getopt/blob/master/src/getopt.erl#L536"&gt;prints usage to stderr&lt;/a&gt; instead of stdout. A quick fix to call getopt:usage/3 with `standard&lt;sub&gt;in&lt;/sub&gt;` as the third argument and the results showed up as expected.
&lt;/p&gt;

&lt;p&gt;
I'm sure there are some other tricks to getting org-babel to get stderr output into the results but from my cursory searching it looked a bit more convoluted and not worth the time when I could fix it with such a small change.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;</description><category>emacs erlang escript org-mode orgmode org-babel</category><guid>https://jamesaimonetti.com/posts/org-babel-and-erlang-escripts/</guid><pubDate>Thu, 06 Oct 2016 03:25:41 GMT</pubDate></item></channel></rss>