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
#1
Hello

I am facing a very unique issue. I have written the following code which will be called by a Tool and pass certain arguments. I was using argpass thinking it will be easy. But I am not able to figure out what is going wrong with it

if __name__ == "__main__":
    log_path = "/var/log/tooly2/"
    log_file = "notifications.log"
    log_file_path = log_path + log_file
    if os.access(log_path, os.W_OK) and os.access(log_path + log_file, os.W_OK):
        # Initialize the default/basic and a custom logger
        logging.basicConfig(filename=log_file_path, level=logging.DEBUG, format='%(process)d %(asctime)s %(levelname)s - %(funcName)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
    else:
        print ("The log directory or the log file is not writable for this script, so no logging is possible. Exiting now.")
        sys.exit(1)
    try:
        n = len(sys.argv)
        logging.debug('Total arguments passed: %s', n) #check the arguments
        logging.debug('I can print the first argument %s', sys.argv[1]) #check arguments 1
        logging.debug('I can print the first argument %s', sys.argv[2]) #check argument 2
        parser = argparse.ArgumentParser()
        parser.add_argument("-N", "--host", help="Node Name") #taken
        parser.add_argument("-I", "--IPAddress", help="Node Name") #taken
        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") #Taken
        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") #taken
        parser.add_argument("-P", "--priority", help="Priority") #taken
        parser.add_argument("-O", "--output", help="Description") #taken
        parser.add_argument("-T", "--eventtime", help="Event Time") #taken
        parser.add_argument("-OS", "--os", help="Operating System") #taken
        parser.add_argument("-OP", "--operator", help="Operator") #taken
        parser.add_argument("-NT", "--notificationtype", help="Notification Type Problem or Clear") #taken
        parser.add_argument("-ND", "--notification_default", help="Define which Operator") #taken
        parser.add_argument("-NM", "--groupoperatoros", help="OS Group") #taken
        parser.add_argument("-NO", "--groupoperatormw", help="Middleware Group") #taken
        arguments = parser.parse_args()
        logging.debug('I am not able to reach here')
    except Exception as e:
        logging.debug('This message should go to the log file %s', e )
        exit(1)
In the logs I am getting

Output:
5618 2022-03-15 12:36:50 DEBUG - <module>: Total arguments passed: 35 5618 2022-03-15 12:36:50 DEBUG - <module>: I can print the first argument key --host 5618 2022-03-15 12:36:50 DEBUG - <module>: I can print the second argument value ccccccccccc85 5629 2022-03-15 12:37:10 DEBUG - <module>: Total arguments passed: 35 5629 2022-03-15 12:37:10 DEBUG - <module>: I can print the first argument key --host 5629 2022-03-15 12:37:10 DEBUG - <module>: I can print the second argument value xxxxxxxxxx190 5633 2022-03-15 12:37:26 DEBUG - <module>: Total arguments passed: 35
So as you see the line
logging.debug('I am not able to reach here') is not printing which means the code is getting lost somewhere in the argpass

Not sure how to even troubleshoot and figure out the issue

Any direction will be very helpful Wall
Reply


Messages In This Thread
Not able to figure out what is wrong with argparse - by radioactive9 - Mar-15-2022, 11:47 AM

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