Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
256 lines (205 sloc) 8.46 KB
<!DOCTYPE html>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=1024, user-scalable=no">
<title>Quick Argparse HowTo</title>
<!-- Required stylesheet -->
<link rel="stylesheet" media="screen" href="core/deck.core.css">
<!-- Extension CSS files go here. Remove or add as needed. -->
<link rel="stylesheet" media="screen" href="extensions/goto/deck.goto.css">
<link rel="stylesheet" media="screen" href="extensions/menu/">
<link rel="stylesheet" media="screen" href="extensions/navigation/deck.navigation.css">
<link rel="stylesheet" media="screen" href="extensions/status/deck.status.css">
<link rel="stylesheet" media="screen" href="extensions/scale/deck.scale.css">
<!-- Style theme. More available in /themes/style/ or create your own. -->
<!-- <link rel="stylesheet" media="screen" href="themes/style/web-2.0.css"> -->
<link rel="stylesheet" media="screen" href="themes/style/swiss.css">
<!-- Transition theme. More available in /themes/transition/ or create your own. -->
<link rel="stylesheet" media="screen" href="themes/transition/horizontal-slide.css">
<!-- Basic black and white print styles -->
<link rel="stylesheet" media="print" href="core/print.css">
<!-- Required Modernizr file -->
<script src="modernizr.custom.js"></script>
<!-- syntax highlighting -->
<link rel="stylesheet" href="styles/monokai_sublime.css">
<script src="highlight.pack.js"></script>
<div class="deck-container">
<!-- Begin slides. Just make elements with a class of slide. -->
<section class="slide">
<section class="slide">
<h2>from pyugrm import speaker</h2>
<pre><code class="python">&gt;&gt;&gt; print(
Niko Wenselowski
&gt;&gt;&gt; print(speaker.job)
Developer at uib gmbh
&gt;&gt;&gt; print(speaker.twitter)
&gt;&gt;&gt; print(speaker.github_url)
&gt;&gt;&gt; print(speaker.notable_things)
Organises the Python Stammtisch Darmstadt
<section class="slide">
<li>parses command-line arguments
<li>builtin since Python 2.7
<li>Obsoletes <em>getopt</em> and <em>optparse</em>
<li>Easy to convert from <em>optparse</em> to <em>argparse</em>
<div class="slide">
<li>The Future Is Here
<li>optparse is deprecated
<li>Parsing the commandline is fun again
<section class="slide">
<pre><code class="python">import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', help='a short option')
parser.add_argument('--long-opt', help='a long option')
parser.add_argument('-b', '--both', help='both')
parser.add_argument('-t', '--tri', '-3', help='and more!')
args = parser.parse_args()</code></pre>
<pre class="slide"><code class="python">usage: [-h] [-s S] [--long-opt LONG_OPT] [-b BOTH] [-t TRI]
optional arguments:
-h, --help show this help message and exit
-s S a short option
--long-opt LONG_OPT a long option
-b BOTH, --both BOTH both
-t TRI, --tri TRI, -3 TRI
and more!</code></pre>
<section class="slide">
<h2>Positional arguments</h2>
<pre><code class="python">import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
<pre class="slide"><code class="python">usage: [-h] first second
positional arguments:
optional arguments:
-h, --help show this help message and exit</code></pre>
<pre class="slide"><code class="python">$ python3 eins
usage: [-h] first second error: the following arguments are required: second</code></pre>
<section class="slide">
<h2>Customising an ArgumentParser</h2>
<pre><code class="python">parser = argparse.ArgumentParser(prog="program name",
description="Doing amazing things!",
epilog="Still amazing after the help.",
args = parser.parse_args()
print (args.thingy)</code></pre>
<pre class="slide"><code>$ python3 \*h
usage: program name [*h] [**thingy THINGY]
Doing amazing things!
optional arguments:
*h, **help show this help message and exit
**thingy THINGY
Still amazing after the help.</code></pre>
<section class="slide">
<h2>Customising Arguments</h2>
<pre><code class="python">parser = argparse.ArgumentParser()
parser.add_argument('--location', default="CCC FFM HQ")
parser.add_argument('--no-ug', action="store_false", help="Disable usergroup.")
parser.add_argument('-a', '--attendee', dest="attendees", action="append")
args = parser.parse_args()
print (args.attendees)</code></pre>
<pre class="slide"><code>$ python3 -a foo -a bar
['foo', 'bar']</code></pre>
<section class="slide">
<pre><code class="python">parser = argparse.ArgumentParser()
parser.add_argument('--speaker', nargs=2)
parser.add_argument('--visitors', type=int)
parser.add_argument('-a', '--attendee', dest="attendees", nargs='*')
args = parser.parse_args()</code></pre>
<pre class="slide"><code>$ python3 --speaker Niko
usage: [-h] [--speaker SPEAKER SPEAKER] [--visitors VISITORS]
[-a [ATTENDEES [ATTENDEES ...]]] error: argument --speaker: expected 2 arguments</code></pre>
<pre class="slide"><code>$ python3 --speaker Niko W. --visitors bla
usage: [-h] [--speaker SPEAKER SPEAKER] [--visitors VISITORS]
[-a [ATTENDEES [ATTENDEES ...]]] error: argument --visitors: invalid int value: 'bla'</code></pre>
<pre class="slide"><code>$ python3 -a foo bar baz banane
['foo', 'bar', 'baz', 'banane']</code></pre>
<section class="slide">
<pre><code class="python">parser = argparse.ArgumentParser()
parser.add_argument('--chaos', choices=['CHAOS', 'nope'])
parser.add_argument('--destroy', action="store_true", required=True)
args = parser.parse_args()</code></pre>
<pre class="slide"><code>$ python3 -h
usage: [-h] [--chaos {CHAOS,nope}] --destroy
optional arguments:
-h, --help show this help message and exit
--chaos {CHAOS,nope}
<pre class="slide"><code>$ python3 --chaos lol
usage: [-h] [--chaos {CHAOS,nope}] --destroy error: argument --chaos: invalid choice: 'lol' (choose from 'CHAOS', 'nope')</code></pre>
<pre class="slide"><code>$ python3 --chaos CHAOS
usage: [-h] [--chaos {CHAOS,nope}] --destroy error: the following arguments are required: --destroy</code></pre>
<!-- End slides. -->
<!-- Begin extension snippets. Add or remove as needed. -->
<!-- deck.navigation snippet -->
<div aria-role="navigation">
<a href="#" class="deck-prev-link" title="Previous">&#8592;</a>
<a href="#" class="deck-next-link" title="Next">&#8594;</a>
<!-- deck.status snippet -->
<p class="deck-status" aria-role="status">
<span class="deck-status-current"></span>
<span class="deck-status-total"></span>
<!-- deck.goto snippet -->
<form action="." method="get" class="goto-form">
<label for="goto-slide">Go to slide:</label>
<input type="text" name="slidenum" id="goto-slide" list="goto-datalist">
<datalist id="goto-datalist"></datalist>
<input type="submit" value="Go">
<!-- End extension snippets. -->
<!-- Required JS files. -->
<script src="jquery.min.js"></script>
<script src="core/deck.core.js"></script>
<!-- Extension JS files. Add or remove as needed. -->
<script src="extensions/menu/"></script>
<script src="extensions/goto/deck.goto.js"></script>
<script src="extensions/status/deck.status.js"></script>
<script src="extensions/navigation/deck.navigation.js"></script>
<script src="extensions/scale/deck.scale.js"></script>
<!-- Initialize the deck. You can put this in an external file if desired. -->
$(function() {