• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: python getopts option not matching on operator ‘==’

Resolved: python getopts option not matching on operator ‘==’

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

following some examples from https://www.programcreek.com/python/example/121/getopt.getopt where they make option comparisons with == i.e (option == “–json”), I am trying to enter the if statement. However option will not match despite being of type string and “–json”. Can anyone see what is going wrong?
$ python test.py –json data
–json
<class ‘str’>

Answer:

You are using the longopts parameter incorrectly. Using an = in the option indicates that the option takes arguments but you are not passing your data argument to the --json option, you are passing it to the script as a global argument (because you omitted the = from the --json option).
Note also that you must remove the whitespace between the option and its argument. Finally, you should never use a bare except.
Usage:

If you have better answer, please add a comment about this, thank you!

python sys
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Visual Studio 2022 crashes when using breakpoints

24/03/2023

Resolved: How to get Union type from an array of objects in Flow?

24/03/2023

Resolved: Modify entity using Action in C#

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.