Python Forum
Not able to figure out what is wrong with argparse
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Not able to figure out what is wrong with argparse
#4
Your problem is your debugging code.
The debugging code had a bug. If no arguments are supplied, the index access sys.argv[1] will throw the exception.
Catching all Exceptions with Exception is wrong. Instead, let fail your code in the first place.
Then look which Exceptions could occur and which Exceptions you expect to catch.

import argparse


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("-N", "--host", help="Node Name")
    parser.add_argument("-I", "--IPAddress", help="Node Name")
    parser.add_argument("-S", "--service", help="Service Name")
    parser.add_argument("-D", "--displayname", help="Display Name")
    parser.add_argument("-G", "--group", help="Group")
    parser.add_argument("-E", "--environment", help="Environement")
    parser.add_argument("-Z", "--newstate", help="Current Type of the alert")
    parser.add_argument("-A", "--appname", help="Application Name")
    parser.add_argument("-HA", "--hostappname", help="Host App Name")
    parser.add_argument("-P", "--priority", help="Priority")
    parser.add_argument("-O", "--output", help="Description")
    parser.add_argument("-T", "--eventtime", help="Event Time")
    parser.add_argument("-OS", "--os", help="Operating System")
    parser.add_argument("-OP", "--operator", help="Operator")
    parser.add_argument(
        "-NT", "--notificationtype", help="Notification Type Problem or Clear"
    )
    parser.add_argument("-ND", "--notification_default", help="Define which Operator")
    parser.add_argument("-NM", "--groupoperatoros", help="OS Group")  # taken
    parser.add_argument("-NO", "--groupoperatormw", help="Middleware Group")
    return parser.parse_args()


if __name__ == "__main__":
    args = get_args()
    print(args)


# This error came from line 14
# Line 14: logging.debug('I can print the first argument %s', sys.argv[1]) #check arguments 1
# 14927 2022-03-15 14:18:52 DEBUG - <module>: This message should go to the log file list index out of range

# conclusion, your debugging code introduced new bugs
If you want to use short and long arguments which are required, then you can add required=True.
import argparse


parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", required=True)

# simulating the commandline
args = parser.parse_args(["-o", "test_OUTPUT"])
print(args)


# This here will raise an SystemExit exception because of wrong input
# usually the program will then exit with the supplied code
try:
    parser.parse_args([])
except SystemExit as e:
    print("Method parse_args wanted to exit the programm with return_code", e.code)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Not able to figure out what is wrong with argparse - by DeaD_EyE - Mar-15-2022, 01:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 839 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,694 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  argparse --help in one line. Denial 1 2,041 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,950 Sep-11-2020, 07:42 PM
Last Post: buran
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,179 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,305 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,670 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  python gives wrong string length and wrong character thienson30 2 3,071 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Why am I getting KeyError 'file' when using argparse? Mike Ru 1 3,125 Jun-09-2019, 04:48 PM
Last Post: metulburr
  How can I get some arguments using argparse? Mike Ru 0 1,913 Jun-05-2019, 12:57 PM
Last Post: Mike Ru

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020