|
1224 | 1224 | "source": [
|
1225 | 1225 | "[Link to Camelot](https://bit.ly/3xPBw6L)."
|
1226 | 1226 | ]
|
| 1227 | + }, |
| 1228 | + { |
| 1229 | + "cell_type": "markdown", |
| 1230 | + "id": "ee14bcfb", |
| 1231 | + "metadata": {}, |
| 1232 | + "source": [ |
| 1233 | + "### Comparing Python Command Line Interface Tools: Argparse, Click, and Typer" |
| 1234 | + ] |
| 1235 | + }, |
| 1236 | + { |
| 1237 | + "cell_type": "code", |
| 1238 | + "execution_count": 2, |
| 1239 | + "id": "9e491f2a", |
| 1240 | + "metadata": { |
| 1241 | + "tags": [ |
| 1242 | + "remove-cell" |
| 1243 | + ] |
| 1244 | + }, |
| 1245 | + "outputs": [ |
| 1246 | + { |
| 1247 | + "name": "stdout", |
| 1248 | + "output_type": "stream", |
| 1249 | + "text": [ |
| 1250 | + "/Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter6/command_line_tools\n" |
| 1251 | + ] |
| 1252 | + } |
| 1253 | + ], |
| 1254 | + "source": [ |
| 1255 | + "%mkdir command_line_tools\n", |
| 1256 | + "%cd command_line_tools" |
| 1257 | + ] |
| 1258 | + }, |
| 1259 | + { |
| 1260 | + "cell_type": "markdown", |
| 1261 | + "id": "2d7dcd09", |
| 1262 | + "metadata": {}, |
| 1263 | + "source": [ |
| 1264 | + "Python has several tools for creating CLIs, each with its own approach and syntax. Three popular choices for parsing command-line arguments and options are argparse, click, and typer.\n", |
| 1265 | + "\n", |
| 1266 | + "- **argparse**: Requires manual argument parsing setup with verbose syntax.\n", |
| 1267 | + "- **click**: Offers a more concise and declarative way to define commands with decorators.\n", |
| 1268 | + "- **typer**: Utilizes Python type hints to create a clean and modern CLI interface with minimal boilerplate.\n", |
| 1269 | + "\n", |
| 1270 | + "Here's a comparison of how to create a simple CLI application with a single command that accepts a string argument using argparse, click, and typer." |
| 1271 | + ] |
| 1272 | + }, |
| 1273 | + { |
| 1274 | + "cell_type": "markdown", |
| 1275 | + "id": "5d085ea3", |
| 1276 | + "metadata": {}, |
| 1277 | + "source": [ |
| 1278 | + "argparse:" |
| 1279 | + ] |
| 1280 | + }, |
| 1281 | + { |
| 1282 | + "cell_type": "code", |
| 1283 | + "execution_count": 3, |
| 1284 | + "id": "c4d0cc82", |
| 1285 | + "metadata": {}, |
| 1286 | + "outputs": [ |
| 1287 | + { |
| 1288 | + "name": "stdout", |
| 1289 | + "output_type": "stream", |
| 1290 | + "text": [ |
| 1291 | + "Writing argparse_example.py\n" |
| 1292 | + ] |
| 1293 | + } |
| 1294 | + ], |
| 1295 | + "source": [ |
| 1296 | + "%%writefile argparse_example.py\n", |
| 1297 | + "import argparse\n", |
| 1298 | + "\n", |
| 1299 | + "def main(message):\n", |
| 1300 | + " print(f\"Message: {message}\")\n", |
| 1301 | + "\n", |
| 1302 | + "if __name__ == \"__main__\":\n", |
| 1303 | + " parser = argparse.ArgumentParser(description=\"A simple CLI with argparse\")\n", |
| 1304 | + " parser.add_argument(\"message\", type=str, help=\"The message to print\")\n", |
| 1305 | + " args = parser.parse_args()\n", |
| 1306 | + " main(args.message)" |
| 1307 | + ] |
| 1308 | + }, |
| 1309 | + { |
| 1310 | + "cell_type": "markdown", |
| 1311 | + "id": "337f7e29", |
| 1312 | + "metadata": {}, |
| 1313 | + "source": [ |
| 1314 | + "```bash\n", |
| 1315 | + "python argparse_example.py \"Hello, World!\"\n", |
| 1316 | + "```" |
| 1317 | + ] |
| 1318 | + }, |
| 1319 | + { |
| 1320 | + "cell_type": "code", |
| 1321 | + "execution_count": 4, |
| 1322 | + "id": "923faea1", |
| 1323 | + "metadata": { |
| 1324 | + "tags": [ |
| 1325 | + "remove-input" |
| 1326 | + ] |
| 1327 | + }, |
| 1328 | + "outputs": [ |
| 1329 | + { |
| 1330 | + "name": "stdout", |
| 1331 | + "output_type": "stream", |
| 1332 | + "text": [ |
| 1333 | + "Message: Hello, World!\n" |
| 1334 | + ] |
| 1335 | + } |
| 1336 | + ], |
| 1337 | + "source": [ |
| 1338 | + "!python argparse_example.py \"Hello, World!\"" |
| 1339 | + ] |
| 1340 | + }, |
| 1341 | + { |
| 1342 | + "cell_type": "markdown", |
| 1343 | + "id": "1c9744c4", |
| 1344 | + "metadata": {}, |
| 1345 | + "source": [ |
| 1346 | + "click:" |
| 1347 | + ] |
| 1348 | + }, |
| 1349 | + { |
| 1350 | + "cell_type": "code", |
| 1351 | + "execution_count": 5, |
| 1352 | + "id": "414167b5", |
| 1353 | + "metadata": {}, |
| 1354 | + "outputs": [ |
| 1355 | + { |
| 1356 | + "name": "stdout", |
| 1357 | + "output_type": "stream", |
| 1358 | + "text": [ |
| 1359 | + "Writing click_example.py\n" |
| 1360 | + ] |
| 1361 | + } |
| 1362 | + ], |
| 1363 | + "source": [ |
| 1364 | + "%%writefile click_example.py\n", |
| 1365 | + "import click\n", |
| 1366 | + "\n", |
| 1367 | + "@click.command()\n", |
| 1368 | + "@click.argument(\"message\")\n", |
| 1369 | + "def main(message):\n", |
| 1370 | + " print(f\"Message: {message}\")\n", |
| 1371 | + "\n", |
| 1372 | + "if __name__ == \"__main__\":\n", |
| 1373 | + " main()" |
| 1374 | + ] |
| 1375 | + }, |
| 1376 | + { |
| 1377 | + "cell_type": "markdown", |
| 1378 | + "id": "d99147da", |
| 1379 | + "metadata": {}, |
| 1380 | + "source": [ |
| 1381 | + "```bash\n", |
| 1382 | + "python click_example.py \"Hello, World!\"\n", |
| 1383 | + "```" |
| 1384 | + ] |
| 1385 | + }, |
| 1386 | + { |
| 1387 | + "cell_type": "code", |
| 1388 | + "execution_count": 6, |
| 1389 | + "id": "e37698ed", |
| 1390 | + "metadata": { |
| 1391 | + "tags": [ |
| 1392 | + "remove-input" |
| 1393 | + ] |
| 1394 | + }, |
| 1395 | + "outputs": [ |
| 1396 | + { |
| 1397 | + "name": "stdout", |
| 1398 | + "output_type": "stream", |
| 1399 | + "text": [ |
| 1400 | + "Message: Hello, World!\n" |
| 1401 | + ] |
| 1402 | + } |
| 1403 | + ], |
| 1404 | + "source": [ |
| 1405 | + "!python click_example.py \"Hello, World!\"" |
| 1406 | + ] |
| 1407 | + }, |
| 1408 | + { |
| 1409 | + "cell_type": "markdown", |
| 1410 | + "id": "da174d67", |
| 1411 | + "metadata": {}, |
| 1412 | + "source": [ |
| 1413 | + "typer:" |
| 1414 | + ] |
| 1415 | + }, |
| 1416 | + { |
| 1417 | + "cell_type": "code", |
| 1418 | + "execution_count": 7, |
| 1419 | + "id": "8899e61e", |
| 1420 | + "metadata": {}, |
| 1421 | + "outputs": [ |
| 1422 | + { |
| 1423 | + "name": "stdout", |
| 1424 | + "output_type": "stream", |
| 1425 | + "text": [ |
| 1426 | + "Writing typer_example.py\n" |
| 1427 | + ] |
| 1428 | + } |
| 1429 | + ], |
| 1430 | + "source": [ |
| 1431 | + "%%writefile typer_example.py\n", |
| 1432 | + "import typer\n", |
| 1433 | + "\n", |
| 1434 | + "def main(message: str):\n", |
| 1435 | + " print(f\"Message: {message}\")\n", |
| 1436 | + "\n", |
| 1437 | + "if __name__ == \"__main__\":\n", |
| 1438 | + " typer.run(main)" |
| 1439 | + ] |
| 1440 | + }, |
| 1441 | + { |
| 1442 | + "cell_type": "markdown", |
| 1443 | + "id": "cbb28a70", |
| 1444 | + "metadata": {}, |
| 1445 | + "source": [ |
| 1446 | + "```bash\n", |
| 1447 | + "python typer_example.py \"Hello, World!\"\n", |
| 1448 | + "```" |
| 1449 | + ] |
| 1450 | + }, |
| 1451 | + { |
| 1452 | + "cell_type": "code", |
| 1453 | + "execution_count": 8, |
| 1454 | + "id": "7d03eb28", |
| 1455 | + "metadata": { |
| 1456 | + "tags": [ |
| 1457 | + "remove-input" |
| 1458 | + ] |
| 1459 | + }, |
| 1460 | + "outputs": [ |
| 1461 | + { |
| 1462 | + "name": "stdout", |
| 1463 | + "output_type": "stream", |
| 1464 | + "text": [ |
| 1465 | + "Message: Hello, World!\n" |
| 1466 | + ] |
| 1467 | + } |
| 1468 | + ], |
| 1469 | + "source": [ |
| 1470 | + "!python typer_example.py \"Hello, World!\"" |
| 1471 | + ] |
1227 | 1472 | }
|
1228 | 1473 | ],
|
1229 | 1474 | "metadata": {
|
|
0 commit comments