13

Is there any way I can tell argparse to not eat quotation marks?

For example, When I give an argument with quotes, argparse only takes what's inside of the quotes as the argument. I want to capture the quotation marks as well (without having to escape them on the command line.)

pbsnodes -x | xmlparse -t "interactive-00"

produces

interactive-00

I want

"interactive-00"
Soviut
  • 88,194
  • 49
  • 192
  • 260
denvaar
  • 2,174
  • 2
  • 22
  • 26

3 Answers3

16

I think it is the shell that eats them, so python will actually never see them. Escaping them on the command line may be your only option.

If it's the \"backslash\" style escaping you don't like for some reason, then this way should work instead:

pbsnodes -x | xmlparse -t '"interactive-00"'
Eric
  • 95,302
  • 53
  • 242
  • 374
wim
  • 338,267
  • 99
  • 616
  • 750
  • 2
    This is exactly the case. The shell sees the quotes as "take whatever's in here even if it contains spaces." And it does so. That's what quotes are *for* in the shell. – kindall Oct 31 '12 at 23:06
  • Thanks for the nice solution. – Abhay Dwivedi Oct 20 '18 at 18:14
  • Is it the shell or the c run time library? Shell passes command to the process as is, it's up to the RTL to parse the arguments. I think it's perl's rtl which eats the quotations, or something else down the line. – il--ya Jan 22 '20 at 10:45
  • @JosVerlinde Your comment does not seem right. Argparse doesn't strip quotes: [Try it online!](https://tio.run/##K6gsycjPM/7/PzO3IL@oRCGxKL0gsag4lQtMFinYwkX0HIvSS3NT80oCwDIamlAleokpKfGJUDkNpWwloERRJpAJlQZTIAXFGtHqZYk56rGa@FQoAZUogdX8/w8A "Python 3 – Try It Online") – wim Jun 04 '23 at 16:36
  • @wim - I stand corrected - arparse is innocent - I wrongly assumed that using Jupyter no shell would be involved – Jos Verlinde Jun 05 '23 at 08:23
1

Command line is parsed into argument vector by python process itself. Depending on how python is built, that would be done by some sort of run-time library. For Windows build, that would be most likely MS Visual C++ runtime library. More details about how it parses command line can be found in Visual C++ documentation: Parsing C++ command-Line arguments.

In particular:

  • A string surrounded by double quotation marks ("string") is interpreted as a single argument, regardless of white space contained within. A quoted string can be embedded in an argument.

  • A double quotation mark preceded by a backslash (\") is interpreted as a literal double quotation mark character (").

If you want to see unprocessed command line, on Windows you can do this:

import win32api
print(win32api.GetCommandLine())
il--ya
  • 315
  • 3
  • 7
0

For me

'"TEXT"'

didn't work with powershell, but with cmd it did

Freni
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 18 '23 at 00:00