<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>scienceoss.com &#187; command line</title>
	<atom:link href="http://scienceoss.com/tags/command-line/feed/" rel="self" type="application/rss+xml" />
	<link>http://scienceoss.com</link>
	<description>useful tidbits for using open source software in science</description>
	<lastBuildDate>Wed, 26 May 2010 03:34:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Run an R script</title>
		<link>http://scienceoss.com/run-an-r-script/</link>
		<comments>http://scienceoss.com/run-an-r-script/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 19:40:11 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[R]]></category>
		<category><![CDATA[command line]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=70</guid>
		<description><![CDATA[From the R prompt: source('filename.R') From the shell: R filename.R]]></description>
			<content:encoded><![CDATA[<p>From the R prompt:</p>
<pre class = "prettyprint"><code class = "code">source('filename.R')</code></pre>
<p>From the shell:</p>
<pre class = "prettyprint"><code class = "code">R filename.R</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/run-an-r-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sending command line options to Python scripts</title>
		<link>http://scienceoss.com/sending-command-line-options-to-python-scripts/</link>
		<comments>http://scienceoss.com/sending-command-line-options-to-python-scripts/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 14:44:38 +0000</pubDate>
		<dc:creator>ryan</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[command line]]></category>

		<guid isPermaLink="false">http://scienceoss.com/?p=52</guid>
		<description><![CDATA[If you have some different options in your program and you want to turn them on or off, or feed your functions different arguments, then you can specify all of this from the command line. You can read about the details of the optparser module here, but here are the basics: Create an OptionParser object. [...]]]></description>
			<content:encoded><![CDATA[<p>If you have some different options in your program and you want to turn them on or off, or feed your functions different arguments, then you can specify all of this from the command line.</p>
<p>You can read about the details of the optparser module <a href="http://docs.python.org/lib/module-optparse.html">here</a>, but here are the basics:<span id="more-52"></span></p>
<p>Create an OptionParser object.</p>
<pre class = "prettyprint"><code class = "code">from optparse import OptionParser
parser = OptionParser()</code></pre>
<p>Use <span class="c">parser.add_option()</span> to add options that will be passed to the script from the command line.  For example,</p>
<pre class = "prettyprint"><code class = "code">parser.add_option('--i', action='store', dest='input_file')
(options,args) = parser.parse_args</code></pre>
<p>Each time you add an option, you have to specify:</p>
<ul>
<li>the option flag (say, <span class="c">-i</span> or <span class="c">&#8211;infile</span> for an input file).  One dash for single characters, two dashes for anything longer.</li>
<li>the action.  This is typically store to store the variable, or store_true to use the option as an on-off switch.</li>
<li>the destination.  The option will be stored in an options object (described shortly . .)</li>
</ul>
<p>There are other things you can add to, like defaults and help strings. See the  <a href="http://docs.python.org/lib/module-optparse.html">documentation</a> for more.</p>
<p>To get everything that was sent to the command line into your script, you get tell the <span class="c">OptionParser</span> to get the parse the arguments from the command line like this:</p>
<pre class = "prettyprint"><code class = "code">(options,args) = parser.parse_args()</code></pre>
<p>Now <span class="c">options</span> has attributes named after the destination.  In the example above, I added the command line option, <span class="c">-i</span>, which will store the argument given right after it in the command line in <span class="c">options.input_file</span>.</p>
<p>So if I called my script from the commandline like this:</p>
<pre class = "prettyprint"><code class = "code">python script.py -i mydata.txt</code></pre>
<p>Then in my script, I could access the &#8216;mydata.txt&#8217; that was provided on the commandline using</p>
<pre class = "prettyprint"><code class = "code">options.input_file</code></pre>
<p>Or, to open the file specified on the command line within my script,</p>
<pre class = "prettyprint"><code class = "code">
f = open(options.input_file)
</code></pre>
<p>Here&#8217;s a longer example:</p>
<pre class = "prettyprint"><code class = "code">
import optparser

parser = OptionParser()

parser.add_option("-i",
                         action="store",
                         dest="infile",
                         help="specify the input file")

parser.add_option("-o",
                         action="store",
                         dest="outfile",
                         help="specify the output file")

# This is a boolean (True/False) option.
# I set the default to False; using the --tabs
# option at the command line will make it true.
parser.add_option("--tabs",
                         action="store_true",
                         dest="use_tabs",
                         default=False,
                         help="tell the script to export as tabs")

parser.add_option("--useless",
                         action="store",
                         dest="dummy_variable",
                         help="not used for anything")

(options, args) = parser.parse_args()

# This is how you access the options:

print options.infile
print options.outfile
print options.use_tabs
print options.dummy_variable
</code></pre>
<p>Now if you run this at the command prompt:</p>
<pre class = "prettyprint"><code class = "code">python test.py -i input.txt -o output.txt</code></pre>
<p>then you&#8217;ll see that the options were passed to the script.  If you don&#8217;t specify an option, its default value is None.  You can also set a default value, like I&#8217;ve done for the &#8211;tabs option above.</p>
]]></content:encoded>
			<wfw:commentRss>http://scienceoss.com/sending-command-line-options-to-python-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

