[issue35005] argparse should accept json and yaml argument types
Change by Robert Benson : -- versions: +Python 2.7 ___ Python tracker <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35005] argparse should accept json and yaml argument types
Change by Robert Benson : -- components: Library (Lib) nosy: derelbenkoenig priority: normal severity: normal status: open title: argparse should accept json and yaml argument types type: enhancement ___ Python tracker <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35005] argparse should accept json and yaml argument types
Change by Robert Benson : -- versions: -Python 2.7 ___ Python tracker <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35005] argparse should accept json and yaml argument types
New submission from Robert Benson : Using `argparse`, I wanted to create an argument that was a JSON dictionary. I found that using this in combination with the `fromfile_prefix_args` keyword argument, that the parser assumes that each argument provided in the file must be on a single line. I want the module to be able to support json files that may be pretty-printed. If it is to accept JSON in this manner, it would be not much more effort to implement YAML parsing as well -- ___ Python tracker <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35005] argparse should accept json and yaml argument types
Robert Benson 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 <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com