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
First of all, as the first step
parser.add_argument('directry', help='input directry')
print(args.directry)
Execution result
$ python3 check.py testDir
testDir
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