(Note) Be careful with python argparse

I write it as a (memo) for myself, but I hope some people will find it useful. For the time being, I will start scratching at the level, so I welcome you.

Here are some things to keep in mind when using python's argparse to send information to a class.

Create the following as x.py

#!/usr/bin/env python
import sys
import argparse
import unittest

class testtst(unittest.TestCase):
    def setUp(self):
         pass

PARSER = argparse.ArgumentParser()
PARSER.add_argument("--browser")
# MY_Make an ARGS and send it.
MY_ARGS = PARSER.parse_args()



if __name__ == "__main__":
    """
    #Without this part, an error will occur.
    if sys.argv:
        del sys.argv[1:]
    """
    unittest.main()

If you execute the following, an error will occur.

python x.py --browser Chrome 


usage: x.py [-h] [--broswer BROSWER]
x.py: error: unrecognized arguments: --browser Chrome

Please comment out the part that will result in an error if this part is missing.

Recommended Posts

(Note) Be careful with python argparse
[Note] Operate MongoDB with Python
TensorFlow 2.2 can't be installed with Python 3.8!
[Python3] Be careful with removing character strings (strip, lstrip, rstrip)
Be careful with easy method references
argparse note
Note: Python
[Note] Hello world output with python
[Python] Be careful when using print
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Python note
Be careful with Python's append method
[Introduction to Udemy Python3 + Application] 51. Be careful with default arguments
Be careful of LANG for UnicodeEncodeError when printing Japanese with Python 3
[Note] Get data from PostgreSQL with Python
Note when creating an environment with python
⚠️ Be careful with Python's default argument values ⚠️
[Personal note] Web page scraping with python3
Trying to handle SQLite3 with Python [Note]
Python modules with "-(hyphen)" cannot be removed
Be careful when running CakePHP3 with PHP7.2
Python study note_002
Note: Python Decorator
FizzBuzz with Python3
Python programming note
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python study note_003
python argparse template
Bingo with python
Zundokokiyoshi with python
[Note] openCV + python
Python beginner's note
Excel with Python
Microcomputer with Python
Cast with python
Project cannot be created with Python3.5 (Windows) + django1.7.1
[Note] Export the html of the site with python.
Python> Run with run-time arguments> Use import argparse
[Note] Create a one-line timezone class with python
Note for formatting numbers with python format function
Investigation when import cannot be done with python
Homebrew Vim doesn't start with Python 3.8 error Note
Be careful when working with gzip-compressed text files
Python script to get note information with REAPER
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2