Created
May 23, 2017 06:17
-
-
Save lkluft/099251857aff20f98c711432238d150d to your computer and use it in GitHub Desktop.
The argparse module makes it easy to write user-friendly command-line interfaces.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2017-05-23T06:17:20.567269Z", | |
"end_time": "2017-05-23T06:17:20.646908Z" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import argparse\n\n\n# Create Parser object.\nparser = argparse.ArgumentParser(description='Process some integers.')\n\n# Add number of command line arguments.\nparser.add_argument(\n 'integers',\n metavar='N',\n type=int, nargs='+',\n help='an integer for the accumulator',\n )\nparser.add_argument(\n '--sum',\n dest='accumulate',\n action='store_const',\n const=sum,\n default=max,\n help='sum the integers (default: find the max)',\n )\nparser.add_argument(\n '--verbose', '-v',\n action='store_true',\n default=False,\n help='print the result',\n )\n\n# Parse command line arguments\nargs = parser.parse_args(['-v', '--sum', '7', '-1', '42'])\nif args.verbose:\n print(args.accumulate(args.integers))", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "48\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"ExecuteTime": { | |
"start_time": "2017-05-23T06:17:20.649600Z", | |
"end_time": "2017-05-23T06:17:20.688753Z" | |
}, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "args = parser.parse_args(['--help'])", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "usage: ipykernel_launcher.py [-h] [--sum] [--verbose] N [N ...]\n\nProcess some integers.\n\npositional arguments:\n N an integer for the accumulator\n\noptional arguments:\n -h, --help show this help message and exit\n --sum sum the integers (default: find the max)\n --verbose, -v print the result\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "error", | |
"ename": "SystemExit", | |
"evalue": "0", | |
"traceback": [ | |
"An exception has occurred, use %tb to see the full traceback.\n", | |
"\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 0\n" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"text": "/dev/shm/u237002/anaconda36/envs/python36/lib/python3.6/site-packages/IPython/core/interactiveshell.py:2855: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n", | |
"name": "stderr" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.1", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"toc": { | |
"toc_threshold": 6, | |
"toc_number_sections": true, | |
"toc_cell": false, | |
"toc_window_display": false | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "The argparse module makes it easy to write user-friendly command-line interfaces.", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment