Python hand play (argparse minimum code)

What is this article?

I started argparse. It's so easy ...

So, I wrote the minimum code to check the shape of the argument.

How amazing

I often forget how to call the code that I left behind. It's easy if it can be done with this much.

rem Call without arguments for the time being → Naturally an error → So usage is displayed
...\work> python argparse_sample.py
usage: python argparse_sample.py -input (inputfilepath) -output (outputfilepath)
argparse_sample.py: error: the following arguments are required: -input, -output

Execute rem usage as it is
...\work> python argparse_sample.py -input (inputfilepath) -output (outputfilepath)
inputfilepath=[(inputfilepath)]
outputfilepath=[(outputfilepath)]

code

def main():
    import argparse
    parser = argparse.ArgumentParser(
        prog='argparse_sample.py',
        usage='python argparse_sample.py -input (inputfilepath) -output (outputfilepath)'
    )
    parser.add_argument('-input', required=True)
    parser.add_argument('-output', required=True)
    args = parser.parse_args()
    inputfilepath = args.input
    outputfilepath = args.output
    print(f'inputfilepath=[{inputfilepath}]')
    print(f'outputfilepath=[{outputfilepath}]')


if __name__ == '__main__':
    main()

Impressions

Finally, maybe. I wanted such a function, and finally I started to think "Look for it!". I haven't had the habit of relying on external resources such as libraries made by other companies for a long time, so I finally started to think "search!" Instead of "make!". Well ... I think it's creating a new natural thing for me.

Recommended Posts

Python hand play (argparse minimum code)
Python hand play (division)
Python hand play (two-dimensional list)
Python hand play (Pandas / DataFrame beginning)
Python hand play (calculated full of mordred)
Python hand play (descriptor calculation: serious version)
Play Python async
Play with 2016-Python
python character code
python argparse template
[Python] Algorithm-aware code
Python hand play (let's get started with AtCoder?)
Python hand play (one line notation of if)
Python hand play (interoperability between CSV and PostgreSQL)
Python code acceleration approach
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
Before writing Python code
Play youtube in python
About Python3 character code
Python Requests status code
OpenCV basic code (python)
Python hand play (get column names from CSV file)
Get country code with python
Python with VS Code (Windows 10)
Python code memo for yourself
How to use Python argparse
[Python] Frequently used library code
Debug Python with VS Code
[Python] Play with Discord's Webhook.
2.x, 3.x character code of python
Stop Omxplayer from Python code
Play RocketChat with API / Python
Python frequently used code snippets
Generate QR code in Python
[Python] Sample code for Python grammar
Character code learned in Python
Convert python 3.x code to python 2.x
Document Python code with Doxygen
Python hand play (RDKit descriptor calculation: SDF to CSV using Pandas)