[PYTHON] A simple example of how to use ArgumentParser

I tried using ArgumentParser for the first time in Python.

There were various articles on how to use it, but it took me a long time to find out what I wanted to do in a hurry, so I almost gave up, so I will leave it as a memo.

What I want to achieve is

When the following is executed, all files in testDir will be processed.

$ python3 check.py testDir  --all

When executed without specifying --all, I want to process a specific file among all the files in testDir

$ python3 check.py testDir
Pass the directory as a parameter

First of all, as the first step

parser.add_argument('directry', help='input directry')
print(args.directry)

Execution result

$ python3 check.py testDir
testDir
Also specify the conditions to execute

Next, allow "--all" to be specified as an option.

parser.add_argument('directry', help='input directry')
parser.add_argument('--all', help='Existence check',action='store_true',required=False) 
print(args.directry)
print(args.all)

Execution result

$ python3 check.py testDir --all
testDir
True
$ python3 check.py testDir
testDir
False

Recommended Posts

A simple example of how to use ArgumentParser
A memo of how to use AIST supercomputer ABCI
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Summary of how to use csvkit
[Python] Summary of how to use pandas
How to calculate Use% of df command
[Python2.7] Summary of how to use unittest
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Pandas: A very simple example of DataFrame.rolling ()
[Question] How to use plot_surface of python
How to calculate the volatility of a brand
How to use xml.etree.ElementTree
How to use Python-shell
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
How to use folium (visualization of location information)
How to use tf.data
How to use virtualenv
How to use Seaboan
[Python] How to use two types of type ()
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
[Python] Explains how to use the range function with a concrete example
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
Not much mention of how to use Pickle
Summary of how to use MNIST in Python
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
Learn the flow of Bayesian estimation and how to use Pystan through a simple regression model
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
Create a dataset of images to use for learning
I tried to summarize how to use matplotlib of python
How to use Keras ~ From simple model generation to CNN ~
Memo of how to use properly when combining pandas.DataFrame
How to write a list / dictionary type of Python3