#!/usr/bin/env python

from __future__  import print_function

import argparse
import os
import sys
import urlparse

from bttools import MagnetURI

def parse_options():
    parser = argparse.ArgumentParser(description="Split Magnet URIs into their component parts, and"
                                                 " display them.")
    parser.add_argument('magnet_uri', help='A magnet URI.')
    parser.add_argument('--long-names', action='store_true', help='Use long names for display')

    options = parser.parse_args()
    return options

def main():
    options = parse_options()
    uri = MagnetURI(options.magnet_uri)

    keys = uri.params.keys(long_names=options.long_names)
    keys.sort()

    max_key_length = max(len(k) for k in keys)
    padding        = " " * (max_key_length + 4)
    output_format  = "%%-%d.%ds => " % (max_key_length, max_key_length)

    for key in keys:
        for i,value in enumerate(uri.params.get_all(key), 1):
            if i == 1:
                line_start = output_format % key
            else:
                line_start = padding

            print(line_start + value)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        sys.exit(">> Caught user interrupt. Exiting...")

# vim:set ft=python tw=100:
