+def usage():
+ print 'Usage: %s [options]' % sys.argv[0]
+ print
+ print '%s' % """Options:
+ -h, --help This usage.
+ -a, --address IP for interface. 0 means use DHCP
+ -g, --gateway Default gateway. Optional.
+ -i, --interface Interface to configure.
+ -m, --netmask Netmask to use for interface. Default /24.
+ -n, --nameserver Nameserver to use. Optional.
+ -r, --record Output create commands to stdout."""
+
+def parse_input():
+ options = ()
+
+ try:
+ opts, args = getopt.gnu_getopt(sys.argv[1:],
+ 'ha:g:i:m:n:r', ['help', 'address=', 'gateway=',
+ 'interface=', 'netmask=', 'nameserver=', 'record'])
+ except getopt.GetoptError as err:
+ print str(err)
+ usage()
+ sys.exit(2)
+
+ address = gateway = interface = netmask = nameserver = None
+ record = False
+
+ if opts:
+ for o, a in opts:
+ if o in ('-h', '--help'):
+ usage()
+ sys.exit(0)
+ if o in ('-a', '--address'):
+ address = a
+ elif o in ('-g', '--gateway'):
+ gateway = a
+ elif o in ('-i', '--interface'):
+ interface = a
+ elif o in ('-m', '--netmask'):
+ netmask = a
+ elif o in ('-n', '--nameserver'):
+ nameserver = a
+ elif o in ('-r', '--record'):
+ record = True
+ else:
+ assert False, 'Unhandled option'
+
+ if not address or not interface:
+ print 'Error: missing options'
+ usage()
+ sys.exit(2)
+ if address == '0':
+ address = None
+ if not netmask:
+ netmask = '24'
+
+ options = (interface, address, netmask, gateway, nameserver)
+
+ return options
+