|
14 | 14 | "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." |
| 22 | + ] |
| 23 | + }, |
17 | 24 | {
|
18 | 25 | "cell_type": "code",
|
19 | 26 | "execution_count": 1,
|
20 |
| - "metadata": { |
21 |
| - "collapsed": false |
22 |
| - }, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "def greet(personal, family, title=\"\", polite=False):\n", |
| 31 | + " 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!" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 3, |
| 78 | + "metadata": {}, |
23 | 79 | "outputs": [
|
24 | 80 | {
|
25 | 81 | "name": "stdout",
|
|
34 | 90 | "#!/usr/bin/env python\n",
|
35 | 91 | "from argparse import ArgumentParser\n",
|
36 | 92 | "\n",
|
| 93 | + "def greet(personal, family, title=\"\", polite=False):\n", |
| 94 | + " greeting = \"How do you do, \" if polite else \"Hey, \"\n", |
| 95 | + " if title:\n", |
| 96 | + " greeting += f\"{title} \"\n", |
| 97 | + "\n", |
| 98 | + " greeting += f\"{personal} {family}.\"\n", |
| 99 | + " return greeting\n", |
| 100 | + "\n", |
37 | 101 | "if __name__ == \"__main__\":\n",
|
38 | 102 | " parser = ArgumentParser(description=\"Generate appropriate greetings\")\n",
|
39 | 103 | " parser.add_argument('--title', '-t')\n",
|
|
42 | 106 | " parser.add_argument('family')\n",
|
43 | 107 | " arguments= parser.parse_args()\n",
|
44 | 108 | " \n",
|
45 |
| - " greeting = \"How do you do, \" if arguments.polite else \"Hey, \"\n", |
46 |
| - " if arguments.title:\n", |
47 |
| - " greeting += f\"{arguments.title} \"\n", |
48 |
| - " greeting += f\"{arguments.personal} {arguments.family}.\"\n", |
49 |
| - " print(greeting)\n" |
| 109 | + " message = greet(arguments.personal, arguments.family,\n", |
| 110 | + " arguments.title, arguments.polite)\n", |
| 111 | + " print(message)" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 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?)." |
50 | 119 | ]
|
51 | 120 | },
|
52 | 121 | {
|
53 | 122 | "cell_type": "markdown",
|
54 | 123 | "metadata": {},
|
55 | 124 | "source": [
|
56 |
| - "If you are using MacOS or Linux, you do the following to create an executable:" |
| 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:" |
57 | 126 | ]
|
58 | 127 | },
|
59 | 128 | {
|
60 | 129 | "cell_type": "code",
|
61 |
| - "execution_count": 2, |
62 |
| - "metadata": { |
63 |
| - "collapsed": true |
64 |
| - }, |
| 130 | + "execution_count": 4, |
| 131 | + "metadata": {}, |
65 | 132 | "outputs": [],
|
66 | 133 | "source": [
|
67 | 134 | "%%bash\n",
|
|
77 | 144 | },
|
78 | 145 | {
|
79 | 146 | "cell_type": "code",
|
80 |
| - "execution_count": 3, |
81 |
| - "metadata": { |
82 |
| - "collapsed": false |
83 |
| - }, |
| 147 | + "execution_count": 5, |
| 148 | + "metadata": {}, |
84 | 149 | "outputs": [
|
85 | 150 | {
|
86 |
| - "name": "stdout", |
| 151 | + "name": "stderr", |
87 | 152 | "output_type": "stream",
|
88 | 153 | "text": [
|
89 | 154 | "usage: greeter.py [-h] [--title TITLE] [--polite] personal family\n",
|
90 |
| - "\n", |
91 |
| - "Generate appropriate greetings\n", |
92 |
| - "\n", |
93 |
| - "positional arguments:\n", |
94 |
| - " personal\n", |
95 |
| - " family\n", |
96 |
| - "\n", |
97 |
| - "optional arguments:\n", |
98 |
| - " -h, --help show this help message and exit\n", |
99 |
| - " --title TITLE, -t TITLE\n", |
100 |
| - " --polite, -p\n" |
| 155 | + "greeter.py: error: the following arguments are required: personal, family\n" |
101 | 156 | ]
|
102 | 157 | }
|
103 | 158 | ],
|
104 | 159 | "source": [
|
105 |
| - "%%bash\n", |
106 |
| - "./greeter.py --help" |
| 160 | + "%%bash --no-raise-error\n", |
| 161 | + "./greeter.py" |
107 | 162 | ]
|
108 | 163 | },
|
109 | 164 | {
|
110 | 165 | "cell_type": "markdown",
|
111 | 166 | "metadata": {},
|
112 | 167 | "source": [
|
113 |
| - "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", |
| 169 | + "\n", |
114 | 170 | "```\n",
|
115 | 171 | "%%cmd\n",
|
116 | 172 | "python greeter.py John Cleese\n",
|
|
119 | 175 | },
|
120 | 176 | {
|
121 | 177 | "cell_type": "code",
|
122 |
| - "execution_count": 4, |
123 |
| - "metadata": { |
124 |
| - "collapsed": false |
125 |
| - }, |
| 178 | + "execution_count": 6, |
| 179 | + "metadata": {}, |
126 | 180 | "outputs": [
|
127 | 181 | {
|
128 | 182 | "name": "stdout",
|
|
137 | 191 | "./greeter.py John Cleese"
|
138 | 192 | ]
|
139 | 193 | },
|
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "We can then use the optional arguments as:" |
| 199 | + ] |
| 200 | + }, |
140 | 201 | {
|
141 | 202 | "cell_type": "code",
|
142 |
| - "execution_count": 5, |
143 |
| - "metadata": { |
144 |
| - "collapsed": false |
145 |
| - }, |
| 203 | + "execution_count": 7, |
| 204 | + "metadata": {}, |
146 | 205 | "outputs": [
|
147 | 206 | {
|
148 | 207 | "name": "stdout",
|
|
159 | 218 | },
|
160 | 219 | {
|
161 | 220 | "cell_type": "code",
|
162 |
| - "execution_count": 6, |
163 |
| - "metadata": { |
164 |
| - "collapsed": false |
165 |
| - }, |
| 221 | + "execution_count": 8, |
| 222 | + "metadata": {}, |
166 | 223 | "outputs": [
|
167 | 224 | {
|
168 | 225 | "name": "stdout",
|
|
183 | 240 | "source": [
|
184 | 241 | "Yes, [he is](https://en.wikipedia.org/wiki/John_Cleese#Honours_and_tributes)!"
|
185 | 242 | ]
|
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "markdown", |
| 246 | + "metadata": {}, |
| 247 | + "source": [ |
| 248 | + "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`." |
| 285 | + ] |
| 286 | + }, |
| 287 | + { |
| 288 | + "cell_type": "code", |
| 289 | + "execution_count": 10, |
| 290 | + "metadata": {}, |
| 291 | + "outputs": [ |
| 292 | + { |
| 293 | + "name": "stdout", |
| 294 | + "output_type": "stream", |
| 295 | + "text": [ |
| 296 | + "Overwriting greeter.py\n" |
| 297 | + ] |
| 298 | + } |
| 299 | + ], |
| 300 | + "source": [ |
| 301 | + "%%writefile greeter.py\n", |
| 302 | + "#!/usr/bin/env python\n", |
| 303 | + "from argparse import ArgumentParser\n", |
| 304 | + "\n", |
| 305 | + "def greet(personal, family, title=\"\", polite=False):\n", |
| 306 | + " greeting = \"How do you do, \" if polite else \"Hey, \"\n", |
| 307 | + " if title:\n", |
| 308 | + " greeting += f\"{title} \"\n", |
| 309 | + "\n", |
| 310 | + " greeting += f\"{personal} {family}.\"\n", |
| 311 | + " return greeting\n", |
| 312 | + "\n", |
| 313 | + "def process():\n", |
| 314 | + " parser = ArgumentParser(description=\"Generate appropriate greetings\")\n", |
| 315 | + "\n", |
| 316 | + " parser.add_argument('--title', '-t')\n", |
| 317 | + " parser.add_argument('--polite', '-p', action=\"store_true\")\n", |
| 318 | + " parser.add_argument('personal')\n", |
| 319 | + " parser.add_argument('family')\n", |
| 320 | + "\n", |
| 321 | + " arguments = parser.parse_args()\n", |
| 322 | + "\n", |
| 323 | + " print(greet(arguments.personal, arguments.family,\n", |
| 324 | + " arguments.title, arguments.polite)) \n", |
| 325 | + "\n", |
| 326 | + "if __name__ == \"__main__\":\n", |
| 327 | + " process()" |
| 328 | + ] |
186 | 329 | }
|
187 | 330 | ],
|
188 | 331 | "metadata": {
|
|
204 | 347 | "name": "python",
|
205 | 348 | "nbconvert_exporter": "python",
|
206 | 349 | "pygments_lexer": "ipython3",
|
207 |
| - "version": "3.5.2" |
| 350 | + "version": "3.8.5" |
208 | 351 | },
|
209 | 352 | "widgets": {
|
210 | 353 | "state": {},
|
|
0 commit comments