This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argparse library: parse --set type
Type: enhancement Stage:
Components: Library (Lib) Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.araujo, fralau
Priority: normal Keywords:

Created on 2018-08-25 06:51 by fralau, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg324043 - (view) Author: (fralau) Date: 2018-08-25 06:51
With argparse, an common requirement is is that a user may want to pass a series of key/value pairs (a sort of equivalent of **args on the command line). Here is a solution that could be implemented in the standard library with little effort.

This can be done so, e.g.:
    command --set foo=hello bar="hello world" baz=5

With the benefit of experience, this is easy and very intuitive.

This does not require any modification, as the '--set' option can be defined as:  
   nargs='+',
   metavar="KEY=VALUE"

The next step is to parse the resulting list.

See github gist for a trivial implementation: https://gist.github.com/fralau/061a4f6c13251367ef1d9a9a99fb3e8d). 

It might be useful to integrate a helper method to the standard library together with an explanation in the doc on how to do it.
msg324438 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2018-08-31 17:18
In my experience, a more common way to say “stop parsing and take the remaining arguments as one value” is using `--`.
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78677
2018-08-31 17:18:26eric.araujosetnosy: + eric.araujo
messages: + msg324438
2018-08-25 06:51:30fralaucreate