[issue35005] argparse should accept json and yaml argument types

Robert Benson report at bugs.python.org
Wed Oct 17 09:54:57 EDT 2018


Robert Benson <derelbenkoenig at gmail.com> added the comment:

What I'm talking about is reading a single arg (of a dictionary or collection type) that can be split across multiple lines, rather than a single line containing multiple args

My motivation was that reading args from a file should behave in a manner similar to other command-line utilities, such as the `-d` option for `curl` and the `-e` option for `ansible`. These take the entire file you give it and store it as one dictionary or object, not by merging it with the rest of the namespace but by taking the dictionary as the value of just that arg. So:

argument_parser.add_argument("-d", "--data", type=argparse.JsonType)  # just for example

if I call the program with `--data @foo.json`
I want argument_parser.parse_args().data to be the dict that is in foo.json, whether foo.json is pretty-printed or not.

I haven't done an exhaustive search of StackOverflow, but seeing a couple top answers indicated that this was not readily available without the user at least having to call `json.loads` on a string argument themselves, when it seems logical that it would be built into the library to parse the json into a dictionary

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35005>
_______________________________________


More information about the Python-bugs-list mailing list