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
#11
Hi

Thanks. The call of the script is coming from a tool which is enforcing the below format as agument. Look at the comma separations between keys and values and quotes. I do not have a way to change that argument passing done by the caller Application

Quote:'--host', 'ccccccc602', '--service', 'SN-WIN-osservices', '--priority', 'WARNING', '--output', 'WARNING: , delayed (BITS=stopped (delayed))', '--os', 'Windows Server 2016 Standard', '--operator', 'operator_os', '--notificationtype', 'PROBLEM', '--notification_default', 'operator_os', '--hostappname', 'TEC-Server', '--Environment', 'PROD', '--eventtime', '2022-03-15 15:13:26 +0100', '--division', 'Tec', '--operatoros', 'G_W_RUN', '--operatormw', 'G_W_RUN', '--appname', 'TEC-Server', '--Info', 'Prueft den Status wichtiger Windows-Services auf Windows-Hosts', '--IPAddress', '99.99.99.99'
Reply
#12
If you had tried the code I wrote above, you would have a log message indicating the error that the parser met.
Reply
#13
(Mar-15-2022, 04:30 PM)Gribouillis Wrote: If you had tried the code I wrote above, you would have a log message indicating the error that the parser met.

Hello Mate
Sorry I am really lost. Don't take it otherwise. I thought it is going to be really simple. Never expected parsing will go beyond control Huh

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--host", help="Node Name") #taken
parser.add_argument("--IPAddress", help="Node Name") #taken
parser.add_argument("--service", help="Service Name")
parser.add_argument("--displayname", help="Display Name")
parser.add_argument("--environment", help="Environement") #Taken
parser.add_argument("--newstate", help="Current Type of the alert")
parser.add_argument("--appname", help="Application Name")
parser.add_argument("--hostappname", help="Host App Name") #taken
parser.add_argument("--priority", help="Priority") #taken
parser.add_argument("--output", help="Description") #taken
parser.add_argument("--eventtime", help="Event Time") #taken
parser.add_argument("--os", help="Operating System") #taken
parser.add_argument("--operator", help="Operator") #taken
parser.add_argument("--notificationtype", help="Notification Type Problem or Clear") #taken
parser.add_argument("--notification_default", help="Define which Operator") #taken
parser.add_argument("--operatoros", help="OS  Group") #taken
parser.add_argument("--operatormw", help="Middleware  Group") #taken
parser.add_argument("--division", help="Division")
parser.add_argument("--Info", help="Additional Information")
 
_error = parser.error
 
def error(message):
    print('argparse parser error %s', message)
    _error(message)
 
parser.error = error 
arguments = parser.parse_args()
Quote:run Testing.py '--host', 'cccccccc0602', '--service', 'SN-WIN-osservices', '--priority', 'WARNING', '--output', 'WARNING: , delayed (BITS=stopped (delayed))', '--os', 'Windows Server 2016 Standard', '--operator', 'operator_os', '--notificationtype', 'PROBLEM', '--notification_default', 'operator_os', '--hostappname', 'TEC-Server', '--Environment', 'PROD', '--eventtime', '2022-03-15 15:13:26 +0100', '--division', 'Sales', '--operatoros', 'G_W_RUN', '--operatormw', 'G_WC_RUN', '--appname', 'TEC-Server', '--Info', 'Prueft den Status wichtiger Windows-Services auf Windows-Hosts', '--IPAddress', '99.99.99.99'

Error:
argparse parser error %s unrecognized arguments: '--host', 'cccccccccc0602', '--service', 'SN-WIN-osservices', '--priority', 'WARNING', '--output', 'WARNING: , delayed (BITS=stopped (delayed))', '--os', 'Windows Server 2016 Standard', '--operator', 'operator_os', '--notificationtype', 'PROBLEM', '--notification_default', 'operator_os', '--hostappname', 'TEC-Server', '--Environment', 'PROD', '--eventtime', '2022-03-15 15:13:26 +0100', '--division', 'Sales', '--operatoros', 'G_W_RUN', '--operatormw', 'G_WC_RUN', '--appname', 'TEC-Server', '--Info', 'Prueft den Status wichtiger Windows-Services auf Windows-Hosts', '--IPAddress', '99.99.99.99' usage: Testing.py [-h] [--host HOST] [--IPAddress IPADDRESS] [--service SERVICE] [--displayname DISPLAYNAME] [--group GROUP] [--environment ENVIRONMENT] [--newstate NEWSTATE] [--appname APPNAME] [--hostappname HOSTAPPNAME] [--priority PRIORITY] [--output OUTPUT] [--eventtime EVENTTIME] [--os OS] [--operator OPERATOR] [--notificationtype NOTIFICATIONTYPE] [--notification_default NOTIFICATION_DEFAULT] [--operatoros OPERATOROS] [--operatormw OPERATORMW] [--division DIVISION] [--Info INFO] Testing.py: error: unrecognized arguments: '--host', 'cccccccccc0602', '--service', 'SN-WIN-osservices', '--priority', 'WARNING', '--output', 'WARNING: , delayed (BITS=stopped (delayed))', '--os', 'Windows Server 2016 Standard', '--operator', 'operator_os', '--notificationtype', 'PROBLEM', '--notification_default', 'operator_os', '--hostappname', 'TEC-Server', '--Environment', 'PROD', '--eventtime', '2022-03-15 15:13:26 +0100', '--division', 'Sales', '--operatoros', 'G_W_RUN', '--operatormw', 'G_WC_RUN', '--appname', 'TEC-Server', '--Info', 'Prueft den Status wichtiger Windows-Services auf Windows-Hosts', '--IPAddress', '99.99.99.99' An exception has occurred, use %tb to see the full traceback. SystemExit: 2
If there is nothing obvious and the suspect is comma separated key value pair in arguments rather than simple --host='cccccccccc0602' and so on - can we think of different approach to parse this?
Reply
#14
It looks like --Environment is misspelled, the parser knows --environment
Reply
#15
(Mar-15-2022, 05:23 PM)Gribouillis Wrote: It looks like --Environment is misspelled, the parser knows --environment

Corrected thanks. Same error still.
If the argument doesn't exist or argument value is None argparse will ignore it - isn't it
and vice versa
A argument is passed but there is no corresponding parser.add_argument line - that value is missed.
This was my understanding. Anyways.

Not sure what is going wrong here. I think argparse understand proper key value pair. Not kind of list

run Testing.py --host='ccccccccc0602' --service='SN-WIN-osservices' --priority='WARNING'
The above simple code works

But my problem is the argument I am getting from the tool cannot be modified and is fixed format with comma separation
Reply
#16
What is this run command that you are using, also why are there commas on the command line ? It looks like you copied and pasted some part of a Python list to build an invalid command line.

Languages have a strict syntax. Python has a syntax, so does argparse and so does the command line. Your code must conform to these syntaxes.
Reply
#17
(Mar-15-2022, 06:09 PM)Gribouillis Wrote: What is this run command that you are using, also why are there commas on the command line ? It looks like you copied and pasted some part of a Python list to build an invalid command line.

Languages have a strict syntax. Python has a syntax, so does argparse and so does the command line. Your code must conform to these syntaxes.

run command is just in the spyder for testing. Please ignore that

Regarding the commas

A monitoring tool (icinga2) basically creates the argument. Then it calls the python script and pass those as arguments
I ran the following line to capture what the tool is passing as arguments

print('Printing all arguments', sys.argv[1:])
And the argument looks just like that.
I do understand that argpass will have strict syntax. Now my obvious question is that I have got that as argument. How do I handle it
May be argpass is not the solution for this. What else i can do?
Reply
#18
Why not strip off the commas?
import sys
import argparse

def parse_args():
    args = [arg.strip(', ') for arg in sys.argv[1:]]
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', '--count')
    parser.add_argument('-n', '--name')
    return parser.parse_args(args)

print(sys.argv)
print(parse_args())
Output:
['junk2.py', '--count', '3,', '--name', 'Steve'] Namespace(count='3', name='Steve')
It is very odd that there is software that separates command line arguments by commas. This not only breaks argparse, but any kind of command line argument processing. Nobody uses commas to separate command line arguments. I really think you do have control over this and something is configured wrong.
Reply
#19
radioactive9 Wrote:I ran the following line to capture what the tool is passing as arguments
Run this one instead
print('Printing all arguments', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:]))
Reply
#20
Honestly you are right

The arguments should not be so odd. Something is really off. I just changed the script to a simple shell script

#!/bin/bash
echo "$@" >> /tmp/icinga_argument.txt
echo "---\n" >> /tmp/icinga_argument.txt

And the output looks like below with no comma

Rest of the configuration is absolutely same. I just changed the script to execute shell script

Quote:--hostappname TEC-W_Server --service SN-WIN-osservices --priority WARNING --output WARNING: , delayed (BITS=stopped (delayed)) --os Windows Server 2016 Standard --operator operator_os --notificationtype PROBLEM --notification_defaultcism operator_os --info Prueft den Status wichtiger Windows-Services auf Windows-Hosts --IPAddress 99.99.99.99 --host ccccccccc0416 --eventtime 2022-03-16 09:13:42 +0100 --environment PROD --division Sales --operatoros GOSC_WCP_RUN --operatormw GOSC_WCP_RUN --appname TEC-W_Server

How come the 2 scripts getting two different arguments even if they are called exactly under same configuration


@Gribouillis Can't make your code work. Don't know why getting syntax error

Error:
File "./Servicenow.py", line 161 print('Printing all arguments with new method', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:])) ^ SyntaxError: invalid syntax
Reply


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