New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expected behavior of argparse given quoted strings #85766
Comments
I'm not sure if this is a bug, but I had a problem when I was trying to use argparse recently, and I was wondering about the expected behavior. For context: We invoke a Python program from a deployment tool, where we provide input in a text box. We were using argparse to read and parse the input arguments. The scenario we had was we were requiring two named arguments to be given, as illustrated in the minimal example below.
When invoking this program from this deployment tool giving As it turns out, the input was provided in the same way as if you had given the program a quoted string in the shell:
When given a quoted string like this, This was a bit annoying! One way to get around it, which we did indeed implement, is to mutate Given that the string contains named arguments, it seems to me that it could be possible, and safe, to unpack this quoted string. Would that make sense? Or am I using it incorrectly? Or is there some other way to provide input such that I don't have to do this hack that I mentioned? If we make a similar program where the arguments
and we call the program as before with |
For what it's worth, I'd love to work on this if it's something that could be nice to have. |
It seems that I mixed up something in the post here. If the quoted string is |
In fact, what happens in the latter case (i.e. Line 2227 in 2ce3963
Here it says that if there's a space in the string, it was meant to be a positional, and so the function returns In conclusion, it seems to me that argparse is not, in fact, meant to handle quoted strings, or rather, strings where there are spaces. |
This is all working as designed. We do not want to modify argparse to split parameters. You probably want to split the input with shlex.split(). See https://stackoverflow.com/questions/44945815/how-to-split-a-string-into-command-line-arguments-like-the-shell-in-python You shouldn't need to mutate sys.argv. You can break the input up into multiple strings with shlex.split() (or whatever you decide to use) and pass those to ArgumentParser.parse_args(). |
I see! Thanks, had not heard about shlex. I also had not realized Thanks a lot! |
I'd say the problem is with the deployment tool. Inputs like that should be split regardless of who's doing the commandline parsing. With normal shell input, quotes are used to prevent splitting, or to otherwise prevent substitutions and special character handling. |
Completely agree with paul j3. The calling tool is breaking the "argv" conventions. If the OP can control the calling tool, it should be fixed there. |
Great idea, thanks! It's open source, so I'll see if I can fix it. On Thu, 20 Aug 2020 at 17:28, Eric V. Smith <report@bugs.python.org> wrote:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: