#!/usr/bin/env python3
#
# Convert a test specification to command-line options

import pscheduler
from validate import spec_is_valid, MAX_SCHEMA

#load spec JSON
spec = pscheduler.json_load(exit_on_error=True, max_schema=MAX_SCHEMA)
if not isinstance(spec, dict):
    pscheduler.fail("Invalid JSON for this operation")

#validate spec
valid, message = spec_is_valid(spec)
if not valid:
    pscheduler.fail(message)


# With a valid spec in hand, we can simply spit out the values one at
# a time.

result = []

for key, value in spec.items():

    # Things that get special handling

    if key == "schema":
        continue

    option = "--%s" % key

    if isinstance(value, bool):
        if value:
            result.append(option)
            continue
        continue

    result.append(option)

    if key == "data-ports":
        result.append("%d-%d" % (value['lower'], value['upper']))
        continue

    result.append(str(value))

pscheduler.succeed_json(result)
