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
#31
(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
Reply
#32
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.
radioactive9 likes this post
Reply


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