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
#21
The argument list that you showed above is not correct either because white space separates arguments, so for example --info Prueft den Status will be understood as 4 different argument, a value Prueft for the option --info and two separate arguments 'den' and 'Status'. Similarly --eventtime 2022-03-16 09:13:42 +0100 will not be correctly parsed because it contains spaces. The correct way to pass this argument would be by quoting the values containing white space characters:
Output:
--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"
radioactive9 Wrote:How come the 2 scripts getting two different arguments even if they are called exactly under same configuration
We can't tell because we don't have the complete code of the scripts to analyse what they do. I suggest that you stay with a Python script and don't mix that with a shell script, which introduces even more language complexity in the problem. Your main issue is that you don't yet master the basics of Python. Have you tried producing the argument list with the code I wrote above?
radioactive9 likes this post
Reply
#22
I was so frustrated I even tried to use docopt - but it is too complex and I left the idea Huh

Then taking your line of thought I removed all the parameters which was being passed from the Tool whose value contains space

Big Grin And voila. It just went through and created the necessary servicenow ticket.

Yes I am absolute noob in python. I know this and that in all coding language - but not mastered any of them. Confused

For example -
I passed all single worded value arguments and all went well
As soon as I introduced --os which has space it stopped working as the value has space. Wall
Further to my testing '--eventtime', '2022-03-16 16:15:16 +0100' actually pursing without any issue. But --os fails :(


So what are my options as --output is very crucial parameter which contain important information and need to be part of the ticket.

(Mar-16-2022, 08:55 AM)Gribouillis Wrote: Have you tried producing the argument list with the code I wrote above?

No I kept getting Error to run that. Those oneliners always confuse me.
Reply
#23
Gribouillis Wrote:The correct way to pass this argument would be BY QUOTING THE VALUES CONTAINING WHITE SPACE characters:
Output:
--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"
The same applies to --os "Windows Server 2016 Standard"

Wikipedia: Quotation Mark
Reply
#24
(Mar-16-2022, 02:34 PM)radioactive9 Wrote: Further to my testing '--eventtime', '2022-03-16 16:15:16 +0100' actually pursing without any issue. But --os fails :(

Even though --eventtime has space but it goes through without problem and double quotes.
I figured out a way to pass '--os', ' "Windows Server 2016 Standard" ' - but it continues to fail

I couldn't run your code Huh and I know that could have helped a lot.
Idea Please help me to dump the exact arguments in the debug log.
Reply
#25
radioactive9 Wrote:I figured out a way to pass '--os', ' "Windows Server 2016 Standard" ' - but it continues to fail
Don't pass
Output:
'--os', ' "Windows Server 2016 Standard" '
Instead pass
Output:
--os "Windows Server 2016 Standard"
Don't add commas and random quotation marks.
Reply
#26
(Mar-16-2022, 03:46 PM)Gribouillis Wrote:
radioactive9 Wrote:I figured out a way to pass '--os', ' "Windows Server 2016 Standard" ' - but it continues to fail
Don't pass
Output:
'--os', ' "Windows Server 2016 Standard" '
Instead pass
Output:
--os "Windows Server 2016 Standard"
Don't add commas and random quotation marks.

Hi I am not doing it. That is what is all confusing things. Exact argument passed by the tool is still elusive.

All I am doing is

    logging.debug('Printing all arguments %s', sys.argv[1:])
output:

Quote:20921 2022-03-16 17:14:45 DEBUG - <module>: Printing all arguments ['--IPAddress', '00.00.00.00.', '--operatormw', 'RESP_APPL_GER', '--operatoros', 'MOBILITY_LINUX', '--division', 'Tech', '--environment', 'DEV', '--eventtime', '2022-03-16 17:14:45 +0100', '--host', 'treeofonion', '--host_appname', 'APP-RIA', '--notificationtype', 'PROBLEM', '--operator', 'operator_os', '--os', '"Red Hat Enterprise Linux 6"', '--priority', 'WARNING', '--service', 'SN-LIN-disk-slash']


And the output in the log is coming as that with comma all over the place and I do not know why.

Even though it is the same
If I remove --os from the tool the same output is working. If I introduce --os in the tool and pass it as argument it doesn't work.
Also I tried to strip the "," but it seems the output in the debug log still has the commas. So not sure exactly what tool is passing and what python is capturing.

Remember when I use a shell script exactly the same way it doesn't have those commas which i have shared earlier.

So now the most important thing is how to dump the arguments as the above code is introducing commas which seems to be not correct
Reply
#27
radioactive9 Wrote:So now the most important thing is how to dump the arguments as the above code is introducing commas which seems to be not correct
I already told you how to dump the arguments (assuming your version of Python is at least 3.6)
print('Printing all arguments', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:]))
I'm afraid I cannot give more help than this line (which must work)
Reply
#28
@radioactive9

This thread has gone on way too long and I think that is due to poor communication. It is very important that you be thorough when describing what you are doing and what you are asking.

This is not a valid command line argument.
Quote:'--os', ' "Windows Server 2016 Standard" '
This is valid.
Quote:--os "Windows Server 2016 Standard"
If you are using the valid command line argument format, do not report you are using the invalid format. Use block quotes like above if you feel the need to for "quoting" your code snippits.

Your questions are vague. Instead of something like this:
Quote:I couldn't run your code Huh and I know that could have helped a lot.
Be sure that you reference what you are talking about.
Quote:I cannot run this code: print('Printing all arguments', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:]))
It generates this error:
Error:
Traceback (most recent call last): File "...y", line 1, in <module> print('Printing all arguments', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:])) NameError: name 'sys' is not defined
Reply
#29
(Mar-16-2022, 04:14 PM)Gribouillis Wrote:
radioactive9 Wrote:So now the most important thing is how to dump the arguments as the above code is introducing commas which seems to be not correct
I already told you how to dump the arguments (assuming your version of Python is at least 3.6)
print('Printing all arguments', ' '.join(f'"{a}"' if ' ' in a else a for a in sys.argv[1:]))
I'm afraid I cannot give more help than this line (which must work)

Version is lower: Does that causing the problem ?
Python 3.4.10
Reply
#30
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:]))
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 699 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,576 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  argparse --help in one line. Denial 1 2,003 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,891 Sep-11-2020, 07:42 PM
Last Post: buran
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,142 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,224 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,601 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,084 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