import collections


def parse_step_options(opts_seq):
    """
    Parse the steps options passed with -o command line parameters.

    :param opts_seq: sequence of command line options as returned by argparse.
                     Their format is: <name or category pattern>.<key>=<value>
                     If <name or cat pattern> is missing, '*' is assumed. It
                     will also be passed to all functions. The pattern is
                     matched using fnmatch.  If <value> is missing, True is
                     assumed.

    :returns: An OrderedDict that maps steps name pattern to a dict of key and
              values.
    """

    # Use an OrderedDict so that the command line order is used to know what
    # option overrides which one.
    options_map = collections.OrderedDict()

    for opt in opts_seq:
        step_k_v = opt.split('=', 1)

        step_k = step_k_v[0]
        step_k = step_k.split('.', 1)
        if len(step_k) >= 2:
            step_pattern, k = step_k
        else:
            # If no pattern was specified, assume '*'
            step_pattern = '*'
            k = step_k[0]

        # Replace '-' by '_' in key names.
        k = k.replace('-', '_')
        v = step_k_v[1] if len(step_k_v) >= 2 else ''
        options_map.setdefault(step_pattern, dict())[k] = v

    return options_map