name for a mutually inclusive relationship

2QdxY4RzWzUUiLuE at potatochowder.com 2QdxY4RzWzUUiLuE at potatochowder.com
Wed Feb 24 16:54:44 EST 2021


On 2021-02-24 at 13:31:42 -0800,
Ethan Furman <ethan at stoneleaf.us> wrote:

> On 2/24/21 1:23 PM, 2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> 
> > > entangled (none or all):
> > > 
> > >     image size override:  height   width
> > 
> > IMO, that's *one* option (-s 640x480 or -s 640,480), not two.  In
> > argparse/optparse terms, a required argument with a custom type.
> > 
> > (OTOH, in a GUI, it'd be two separate mandatory text fields, both
> > controlled (i.e., enabled or disabled) by one checkbox.)
> 
> I didn't say it was a good example.  ;-)  Hopefully it gets the idea across.

Ditto.  ;-)

IMO, the whole idea of "my program has two options, and the user has to
specify both or neither," isn't a question of whether or not the
argument parsing library supports it, but a question of whether or not
it's a good API.

Is the following a good API for a function?

    def override_image_size(image, height=0, width=0):
        '''specify height and width, or set both to 0'''
        if (height == 0 and width != 0) or (height != 0 and width == 0):
            raise ArgumentError('both or neither')
        if height != 0 and width != 0:
            image.height = height
            image.width = width

Or would you more likely write something like this:

    def override_image_size(image, size):
        '''size is a (height, width) tuple'''
        image.height, image.width = size

So why is the former a good API for a command line utility?

Or maybe I don't write command line utilitis with sufficiently complex
argument structures.


More information about the Python-list mailing list