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

import optparse
import pscheduler
import sys

if len(sys.argv) > 1:

   # Args are on the command line
   args = sys.argv[1:]

else:

   # Args are in a JSON array on stdin
   json_args = pscheduler.json_load(exit_on_error=True)
   args = []

   if not isinstance(json_args,list):
      pscheduler.fail("Invalid JSON for this operation")
   for arg in json_args:
      if not ( isinstance(arg, str)
               or isinstance(arg, int)
               or isinstance(arg, float) ):
         pscheduler.fail("Invalid JSON for this operation")
   args = [ str(arg) for arg in json_args ]



# Gargle the arguments

opt_parser = pscheduler.FailingOptionParser(epilog=
"""Examples:

  task dns --query example.net --record soa
    text

"""
)


opt_parser.add_option("--host",
                      help="Host to run the test",
                      action="store", type="string",
                      dest="host")

opt_parser.add_option("--host-node",
                      help="Host to run the test",
                      action="store", type="string",
                      dest="host_node")

opt_parser.add_option("--nameserver",
                      help="Nameserver to query",
                      action="store", type="string",
                      dest="nameserver")

opt_parser.add_option("--record",
                      help="Record type to query  (One of a, aaaa, ns, cname, soa, ptr, mx and txt)",
                      action="store", type="string",
                      dest="record")

opt_parser.add_option("--query",
                      help="String to query",
                      action="store", type="string",
                      dest="query")

opt_parser.add_option("--timeout",
                      help="Timeout for each query attempt",
                      action="store", type="string",
                      dest="timeout")



(options, remaining_args) = opt_parser.parse_args(args)

if len(remaining_args) != 0:
   pscheduler.fail("Unusable arguments: %s" % " ".join(remaining_args))


result = { 'schema': 1 }


if options.host is not None:
   result['host'] = options.host

if options.host_node is not None:
   result['host-node'] = options.host_node

if options.nameserver is not None:
   result['nameserver'] = options.nameserver

if options.record is not None:
   result['record'] = options.record.lower()

if options.query is not None:
   result['query'] = options.query

if options.timeout is not None:
   result['timeout'] = options.timeout


pscheduler.succeed_json(result)
