Python Forum

Full Version: Not able to figure out what is wrong with argparse
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
(Mar-16-2022, 04:22 PM)deanhystad Wrote: [ -> ]f" string formatting is new in Python 3.6. You need to do this:
print('Printing all arguments', ' '.join('"{}"'.format(a) if ' ' in a else a for a in sys.argv[1:]))

Worked like a charm Heart and all the parameters looks very good. If i introduce --os it starts failing. If I remove - it goes forward and creates ticket. Look at --os it does have double quotes around it coming from the tool. even for --eventtime. That was the reason --eventtime never had the problem

Quote:Printing all arguments --IPAddress 00.00.00.00 --operatormw GSC_W_RUN --operatoros GSC_W_RUN --division Tech --environment PROD --eventtime "2022-03-16 17:33:53 +0100" --host wwwwwww754 --host_appname TEC-W --notificationtype RECOVERY --operator operator_os --os "Windows Server 2016 Standard" --priority OK --service SN-WIN-osservices

Another example
Quote:Printing all arguments --IPAddress 99.99.99.99 --operatormw RES_INFRA_NL --operatoros MOBILITY_LINUX --division TEC --environment PROD --eventtime "2022-03-16 17:57:38 +0100" --host fishfishfish --host_appname APPData --notificationtype PROBLEM --os "Red Hat Enterprise Linux 6" --operator operator_mw --priority CRITICAL --service SN-LIN-disk-non_slash


Not sure why I can't parse --os - any suggestion
I really don't see the benefit of the one liner. You get the same information doing this:
print(args)
I do not understand what the problem is with the --os. This works fine for me:
import sys
import argparse

def get_args(args=sys.argv[1:]):
    print("Parsing", args)
    parser = argparse.ArgumentParser()
    parser.add_argument("--os")
    parser.add_argument("--eventtime")
    return parser.parse_args(args)

print(get_args(["--os", "Windows Server 2016 Standard", "--eventtime", "2022-03-16 17:33:53 +0100"]))
print(get_args())
When I call it with these arguments:
Quote:python testargparse.py --os "Windows XP" --eventtime "Right Now"
I get this output.
Output:
Parsing ['--os', 'Windows Server 2016 Standard', '--eventtime', '2022-03-16 17:33:53 +0100'] Namespace(os='Windows Server 2016 Standard', eventtime='2022-03-16 17:33:53 +0100') Parsing ['--os', 'Windows XP', '--eventtime', 'Right Now'] Namespace(os='Windows XP', eventtime='Right Now')
You are still being vague. "If i introduce --os it starts failing". Please include information about how it is failing. Also, it has been a long time since you posted any code. The last I saw was:
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()
I know for sure that this is not the code you are currently running. If you really want help you need to post some updated code.
Pages: 1 2 3 4