# -*- coding: utf-8 -*-

from __future__ import print_function
import subprocess
import sys
import os
import getopt

def usage():
    print("genee-puppet init")
    print("genee-puppet run [-n name] [-p port]")
    print("    default: name=puppetmaster, port=8140")

def puppet_init():
    if not os.path.exists("ssl.d"):
        os.mkdir("ssl.d")
    if not os.path.exists("conf.d"):
        subprocess.check_call("git clone https://bitbucket.org/genee/genee-puppet.git conf.d", shell=True)

def puppet_run():

    try:
        opts, args = getopt.getopt(sys.argv[2:], "n:p:")
    except getopt.GetoptError as e:
        print(str(e))

    name = 'puppetmaster'
    port = 8140
    
    for o, a in opts:
        if o == "-n":
            name = a
        elif o == "-p":
            port = int(a)

    if not os.path.exists('conf.d') or not os.path.exists('ssl.d'):
        print('Run "genee-puppet init" first.')
        sys.exit(0)

    try:
        cmd = ("docker run --name %s -d --restart=always -v %s:/opt/puppet -v %s:/var/lib/puppet/ssl -v /dev/log:/dev/log -p %d:8140 genee/puppetmaster" % (name, os.getcwd() + '/conf.d', os.getcwd() + '/ssl.d', port))
        print(cmd + "\n")
        print(subprocess.check_output(cmd, shell=True, universal_newlines=True))
    except subprocess.CalledProcessError as e:
        return

def main():

    if len(sys.argv) < 2:
        usage()
        sys.exit(1)

    if sys.argv[1] == 'init':
        puppet_init()
    elif sys.argv[1] == 'run':
        puppet_run()
    else:
        usage()
        sys.exit(1)

if __name__ == "__main__":
    main()

@@ -102,22 +102,24 @@
 %0Aimport 
-getopt
+argparse
 %0A%0Adef us
@@ -278,16 +278,20 @@
 et_init(
+args
 ):%0A    i
@@ -511,298 +511,57 @@
 run(
-):%0A%0A    try:%0A        opts, args = getopt.getopt(sys.argv%5B2:%5D, %22n:p:%22)%0A    except getopt.GetoptError as e:%0A        print(str(e))%0A%0A    name = 'puppetmaster'%0A    port = 8140%0A    %0A    for o, a in opts:%0A        if o == %22-n%22:%0A            name = a%0A        elif o == %22-p%22:%0A            port = int(a)
+args):%0A%0A    name = args.name%0A    port = args.port
 %0A%0A  
@@ -1113,85 +1113,763 @@
     
-if len(sys.argv) %3C 2:%0A        usage()%0A        sys.exit(1)%0A%0A    if sys.argv%5B1%5D
+argparser = argparse.ArgumentParser(prog='genee-puppet')%0A%0A    argparser.add_argument('-C', '--path', type=str, default=os.getcwd())%0A%0A    subparsers = argparser.add_subparsers(dest='subcommand')%0A%0A    init_parser = subparsers.add_parser('init')%0A%0A    run_parser = subparsers.add_parser('run')%0A    run_parser.add_argument('-n', '--name', type=str, default='puppetmaster')%0A    run_parser.add_argument('-p', '--port', type=int, default=8140)%0A%0A    help_parser = subparsers.add_parser('help')%0A%0A    args = argparser.parse_args()%0A%0A    if args.subcommand is None:%0A        usage()%0A        sys.exit(1)%0A%0A    cwd = args.path%0A    if cwd and cwd != os.getcwd():%0A        if not os.path.exists(cwd):%0A            os.makedirs(cwd)%0A        os.chdir(cwd)%0A%0A    if args.subcommand
  == 
@@ -1896,16 +1896,20 @@
 et_init(
+args
 )%0A    el
@@ -1915,19 +1915,23 @@
 lif 
-sys.argv%5B1%5D
+args.subcommand
  == 
@@ -1956,16 +1956,20 @@
 pet_run(
+args
 )%0A    el

Replace getopt with argparse for subcommand support.