-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.nw
More file actions
106 lines (77 loc) · 2.05 KB
/
program.nw
File metadata and controls
106 lines (77 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
\documentclass[a4paper,10pt,article,oneside,oldfontcommands]{memoir}
\let\subsubsection\subsection
\let\subsection\section
\let\section\chapter
\input{preamble.tex}
\noweboptions{longxref,breakcode}
\usepackage[noamsthm,notheorems]{beamerarticle}
\setjobnamebeamerversion{slides}
%\usepackage{authblk}
%\let\institute\affil
\title{%
<title>
}
\author{%
<author>
}
\affil{%
<affil>
}
\begin{document}
\maketitle
\begin{abstract}
\input{abstract.tex}
\end{abstract}
\clearpage
\tableofcontents
\clearpage
@
\section{Introduction}
This is the documentation of the [[<<<package>.py>>]] Python 3 script.
It does \dots
\subsection{Structural overview}
We use the standard structure for the script.
We have import statements at the top in the [[<<imports>>]] code block.
Then we have functions in the [[<<functions>>]] code block.
Finally, we have a [[main]] function which is run if the script is run directly
but not if it is imported.
<<<package>.py>>=
#!/usr/bin/env python3
<<imports>>
<<functions>>
def main(argv):
<<main body>>
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
@
Since we rely on the [[sys]] module we need
<<imports>>=
import sys
@
\subsection{Parsing command-line arguments}
The main body is
<<main body>>=
<<parse command-line arguments>>
<<process command-line arguments and do main execution>>
@
To parse command-line arguments we will make use of [[argparse]]:
<<imports>>=
import argparse
@
We first create a parser, then set up the valid arguments and finally provide
the dictionary [[args]] containing each argument that was passed on the command
line.
<<parse command-line arguments>>=
argp = argparse.ArgumentParser(description="<title>")
<<valid argparse arguments>>
args = vars(argp.parse_args(argv[1:]))
@
\subsection{Processing command-line arguments, the main functionality}
We expect a list of arguments on the command line.
<<valid argparse arguments>>=
argp.add_argument("arg", nargs="+", help="An argument to parse")
@
\printbibliography{}
\section{An index of the code blocks}
\nowebchunks
\end{document}