You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"This is the standard library for building programs with a command-line interface. Here we show a short introduction to it, but we recommend to read the [official tutorial](https://docs.python.org/3/howto/argparse.html)."
15
15
]
16
16
},
17
+
{
18
+
"cell_type": "markdown",
19
+
"metadata": {},
20
+
"source": [
21
+
"Let's start by creating a simple `greet` function that accepts some parameters."
" greeting = \"How do you do, \" if polite else \"Hey, \"\n",
32
+
" if title:\n",
33
+
" greeting += f\"{title} \"\n",
34
+
"\n",
35
+
" greeting += f\"{personal} {family}.\"\n",
36
+
" return greeting"
37
+
]
38
+
},
39
+
{
40
+
"cell_type": "markdown",
41
+
"metadata": {},
42
+
"source": [
43
+
"Now we have a function that greets whoever we want."
44
+
]
45
+
},
46
+
{
47
+
"cell_type": "code",
48
+
"execution_count": 2,
49
+
"metadata": {},
50
+
"outputs": [
51
+
{
52
+
"data": {
53
+
"text/plain": [
54
+
"'How do you do, John Cleese.'"
55
+
]
56
+
},
57
+
"execution_count": 2,
58
+
"metadata": {},
59
+
"output_type": "execute_result"
60
+
}
61
+
],
62
+
"source": [
63
+
"greet(\"John\", \"Cleese\", polite=True)"
64
+
]
65
+
},
66
+
{
67
+
"cell_type": "markdown",
68
+
"metadata": {},
69
+
"source": [
70
+
"If we want to create a command line interface for this function, we need to save it on its own file. To add the capability to accept inputs from the command line we are going to use `argparse`.\n",
71
+
"\n",
72
+
"Rememer, what's under the `if __name__ == \"__main__\":` block is what's get executed when you run the file!"
"If you are using MacOS or Linux, you do the following to create an executable:"
118
+
"Note that we've created arguments for each argument `greet` accepts and kept what's optional in the function (the keyword arguments) to be also optional for its command-line interface (can you spot how?)."
119
+
]
120
+
},
121
+
{
122
+
"cell_type": "markdown",
123
+
"metadata": {},
124
+
"source": [
125
+
"We need to tell the computer that this file can be executed to be able to run this script without calling it with `python` everytime. The computer will know what to use by reading the [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) `#!`. If you are using MacOS or Linux, you do the following to create an executable:"
64
126
]
65
127
},
66
128
{
67
129
"cell_type": "code",
68
-
"execution_count": 3,
69
-
"metadata": {
70
-
"collapsed": true
71
-
},
130
+
"execution_count": 4,
131
+
"metadata": {},
72
132
"outputs": [],
73
133
"source": [
74
134
"%%bash\n",
@@ -84,45 +144,29 @@
84
144
},
85
145
{
86
146
"cell_type": "code",
87
-
"execution_count": 10,
88
-
"metadata": {
89
-
"collapsed": false
90
-
},
147
+
"execution_count": 5,
148
+
"metadata": {},
91
149
"outputs": [
92
150
{
93
-
"output_type": "stream",
94
151
"name": "stderr",
152
+
"output_type": "stream",
95
153
"text": [
96
-
"': No such file or directory\n"
97
-
]
98
-
},
99
-
{
100
-
"output_type": "error",
101
-
"ename": "CalledProcessError",
102
-
"evalue": "Command 'b'./greeter.py --help\\n'' returned non-zero exit status 127.",
"\u001b[1;32m<decorator-gen-111>\u001b[0m in \u001b[0;36mshebang\u001b[1;34m(self, line, cell)\u001b[0m\n",
110
-
"\u001b[1;32m~\\anaconda3\\lib\\site-packages\\IPython\\core\\magic.py\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(f, *a, **k)\u001b[0m\n\u001b[0;32m 185\u001b[0m \u001b[1;31m# but it's overkill for just that one bit of state.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 186\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmagic_deco\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 187\u001b[1;33m \u001b[0mcall\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mlambda\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 188\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 189\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcallable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mCalledProcessError\u001b[0m: Command 'b'./greeter.py --help\\n'' returned non-zero exit status 127."
154
+
"usage: greeter.py [-h] [--title TITLE] [--polite] personal family\n",
155
+
"greeter.py: error: the following arguments are required: personal, family\n"
113
156
]
114
157
}
115
158
],
116
159
"source": [
117
-
"%%bash\n",
118
-
"./greeter.py --help"
160
+
"%%bash --no-raise-error\n",
161
+
"./greeter.py"
119
162
]
120
163
},
121
164
{
122
165
"cell_type": "markdown",
123
166
"metadata": {},
124
167
"source": [
125
-
"if you are using Windows, change `bash` by `cmd`, and prepend the commands by `python`\n",
168
+
"if you are using Windows' commands or powershell terminal (instead of git-bash), then the shebang is ignored and you will have to call `python` explicitily. Additionally, for the notebooks cells, you need to change `bash` by `cmd`.\n",
"From the error we got above when we called `greeter.py` without arguments, you may have noticed that in the usage message there's also a `-h` optional argument. We know it's optional because it's shown within square brackes, like for `[--polite]`. This new argument, as the usage message seen above, is generated automatically by argparse and you can use it to see the help."
249
+
]
250
+
},
251
+
{
252
+
"cell_type": "code",
253
+
"execution_count": 9,
254
+
"metadata": {},
255
+
"outputs": [
256
+
{
257
+
"name": "stdout",
258
+
"output_type": "stream",
259
+
"text": [
260
+
"usage: greeter.py [-h] [--title TITLE] [--polite] personal family\n",
261
+
"\n",
262
+
"Generate appropriate greetings\n",
263
+
"\n",
264
+
"positional arguments:\n",
265
+
" personal\n",
266
+
" family\n",
267
+
"\n",
268
+
"optional arguments:\n",
269
+
" -h, --help show this help message and exit\n",
270
+
" --title TITLE, -t TITLE\n",
271
+
" --polite, -p\n"
272
+
]
273
+
}
274
+
],
275
+
"source": [
276
+
"%%bash\n",
277
+
"./greeter.py --help"
278
+
]
279
+
},
280
+
{
281
+
"cell_type": "markdown",
282
+
"metadata": {},
283
+
"source": [
284
+
"Before we move into the next section, let's clean up our `if __name__ == \"__main__\":` block by creating a function that keeps the `argparse` magic. We will call that function `process`."
0 commit comments