<?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>Required &quot;Optional&quot; Parameters Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/required-optional-parameters/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/required-optional-parameters/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Sat, 26 Oct 2024 19:35:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Python Argparse</title>
		<link>https://creatronix.de/python-argparse/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Fri, 19 Mar 2021 12:55:14 +0000</pubDate>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[argparse]]></category>
		<category><![CDATA[Command Line Help]]></category>
		<category><![CDATA[Required "Optional" Parameters]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=3527</guid>

					<description><![CDATA[<p>Primer: Arguments vs Parameters I&#8217;m sometimes confused. Is it an argument or a parameter? So here it goes: A parameter is a variable in a function definition. When a function is called, the arguments are the data you pass into the function&#8217;s parameters. The same goes for a program: A program has parameters, you call&#8230;</p>
<p>The post <a href="https://creatronix.de/python-argparse/">Python Argparse</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Primer: Arguments vs Parameters</h2>
<p>I&#8217;m sometimes confused. Is it an argument or a parameter? So here it goes:</p>
<blockquote><p>A parameter is a variable in a function definition.</p>
<p>When a function is called, the arguments are the data you pass into the function&#8217;s parameters.</p></blockquote>
<p>The same goes for a program: A program has parameters, you <strong>call</strong> a program with arguments.</p>
<h2>Argparse Module</h2>
<p><img fetchpriority="high" decoding="async" src="https://creatronix.de/wp-content/uploads/2021/03/argparse-e1661241731903.jpg" alt="" class="alignnone size-full wp-image-3535" width="500" height="380" srcset="https://creatronix.de/wp-content/uploads/2021/03/argparse-e1661241731903.jpg 500w, https://creatronix.de/wp-content/uploads/2021/03/argparse-e1661241731903-300x228.jpg 300w" sizes="(max-width: 500px) 100vw, 500px" /></p>
<p>Batteries included! The argparse module provides a nice way to parse your program arguments:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>import argparse

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('positional_param')
    parser.add_argument('-o', '--optional_param')
    parser.add_argument('-r', '--required_param', required=True)
    args = parser.parse_args()
    print(args.positional_param)
    print(args.optional_param)
    print(args.required_param)</code></pre>
</div>
<h3>Positional Parameters</h3>
<p>Positional arguments are useful for shorter programs. These parameters do not have a name. You could call them &#8220;unnamed parameters&#8221; as well.</p>
<p>It is important that you keep the right sequence when calling the program:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>parser = argparse.ArgumentParser()
parser.add_argument('positional_param_one')
parser.add_argument('positional_param_two')
args = parser.parse_args()
print(args.positional_param_one)
print(args.positional_param_two)</code></pre>
</div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$ python positional_args.py foo bar
foo
bar</code></pre>
</div>
<h3>Optional Parameters</h3>
<p>Optional parameters have a name you have to use when providing an argument to the program</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>parser.add_argument('--optional_param')</code></pre>
</div>
<p><strong>The double dash indicates an optional parameter</strong></p>
<p>You can provide a shorthand for an optional parameter as well</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>parser.add_argument(<strong>'-o'</strong>, '--optional_param')</code></pre>
</div>
<p>When you leave out an optional argument, the value will be defaulted to None</p>
<p>Two ways of calling a program with optional arguments:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$python optional_args.py
None</code></pre>
</div>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$python optional_args.py --optional_param foobar
foobar</code></pre>
</div>
<h3>Required &#8220;Optional&#8221; Parameters</h3>
<p>Now it gets a bit weird: You can make an optional parameter required.</p>
<p>(another strong argument that these should be named &#8220;named parameters&#8221;)</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-python" data-lang="Python"><code>parser.add_argument('-r', '--required_param', required=True)</code></pre>
<p>If you don not provide the argument you will get:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$ python required_params.py
usage: required_params.py [-h] -r REQUIRED_PARAM
required_params.py: error: the following arguments are required: -r/--required_param</code></pre>
</div>
<p>So better provide the parameter:</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$ python required_params.py -r foobar
foobar</code></pre>
</div>
<h2>Command Line Help</h2>
<p>When you start your script with the -h or &#8211;help parameter, you will get a nice overview of the usage</p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-bash" data-lang="Bash"><code>$ python main.py -h
usage: main.py [-h] [-o OPTIONAL_PARAM] -r REQUIRED_PARAM positional_param

positional arguments:
positional_param

optional arguments:
-h, --help show this help message and exit
-o OPTIONAL_PARAM, ---optional_param OPTIONAL_PARAM
-r REQUIRED_PARAM, --required_param REQUIRED_PARAM</code></pre>
</div>
</div>
<p>The post <a href="https://creatronix.de/python-argparse/">Python Argparse</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
