def limit_count(count, on_device):
    """
    Handling of the optional "count" parameter, common in many commands.
    
    Parameters:
      - count     -- desired number of elements
      - on_device -- number of elements on device
 
    If count is None or 0 return what's on device
    If count > 0 use count, unless it is more than what's on device. 
    If count < 0 that means "abs(count) less than what's on device

    Typical usage: 
       count = limit_count(count, mc.mgrp_get_count())
    """
    if count is None:
        return on_device
    elif count > 0:
        return min(count, on_device)
    else:
        return max(on_device + count, 0)