def main():
    optparser = OptionParser(usage="usage: %prog [options] template-file [template variables ...]")
    optparser.add_option('', '--help-examples', dest='examples', action='store_true',
                         help='Show usage examples', default=False)
    optparser.add_option('-l', '--list-variables', dest='listvariables', action='store_true',
                         help='List template variables', default=False)
    optparser.add_option('-f', '--force', dest='force', action='store_true',
                         help='Replace unset variables with UNSET and continue', default=False)
    optparser.add_option('-i', '--input-csv', dest='inputcsv',
                         help='Read variables from a CSV file')
    optparser.add_option('-g', '--generate-csv-header', dest='generatecsvheader', action='store_true',
                         help='Generate the CSV header for an input file based on a given template', default=False)
    optparser.add_option('-F', '--csv-field-separator', dest='csvfieldseparator', default=',',
                         help='Sets the field separator for the CSV header output')
    #optparser.add_option('-D', '--debug', dest='debug', action='store_true',
    #                        help='Enable debug mode', default=False)

    (options, args) = optparser.parse_args()

    #if options.debug:
    #    logger.setLevel(logging.DEBUG)

    #--------------------------------------------------------------------------
    # Show usage examples
    #--------------------------------------------------------------------------
    if options.examples:
        show_usage_examples()
        sys.exit(0)

    #--------------------------------------------------------------------------
    # Argument error handling
    #--------------------------------------------------------------------------
    if not len(args) > 0:
        optparser.print_help()
        sys.exit(-1)
    else:
        if not os.path.exists(args[0]):
            sys.stderr.write("Template file \"%s\" does not exist!\n" % args[0])
            sys.exit(-1)

    #--------------------------------------------------------------------------
    # List template variables
    #--------------------------------------------------------------------------
    if options.listvariables:
        tpl = ConfPlate()
        tpl.templatepath = os.path.dirname(args[0])
        tpl.templatename = os.path.basename(args[0])

        cli = Cli()
        cli.list_template_vars(tpl)

        sys.exit(0)

    #--------------------------------------------------------------------------
    # Generate CSV header template
    #--------------------------------------------------------------------------
    if options.generatecsvheader:
        tpl = ConfPlate()
        tpl.templatepath = os.path.dirname(args[0])
        tpl.templatename = os.path.basename(args[0])

        cli = Cli()
        cli.generate_csv_header(tpl, separator=options.csvfieldseparator)

        sys.exit(0)

    #--------------------------------------------------------------------------
    # Render template
    #--------------------------------------------------------------------------
    tpl = ConfPlate()
    tpl.templatepath = os.path.dirname(args[0])
    tpl.templatename = os.path.basename(args[0])

    cli = Cli()

    # Read variables from CSV file
    if options.inputcsv:
        i = 1

        csvfilename = options.inputcsv

        if not os.path.exists(csvfilename):
            sys.stderr.write("CSV input file \"%s\" does not exist!\n" % csvfilename)
            sys.exit(-1)

        tplvarlist = tpl.get_vardicts_from_csv(csvfilename)

        for e in tplvarlist:
            if options.force:
                tpl.set_variables(e, unset='UNSET')
                print(tpl.render_template())
            else:
                unsetvars = tpl.set_variables(e)

                print(tpl.render_template())

            if len(unsetvars) > 0:
                print("CSV row %i" % i)
                cli.list_unset_vars(unsetvars)
                sys.exit(-1)

        sys.exit(0)

    # Read variables from cmdline
    if len(args) > 1:
        tplvars, failed = cli.cli_vars_to_dict(args[1:])

        if len(failed) > 0:
            sys.stderr.write('\nUnabled to parse the following variables\n\n')

            for e in failed:
                sys.stderr.write("\t%s\n" % e)

            sys.stderr.write('\nVariable/Value pairs are separated by the equals sign\n\n')
            sys.stderr.write('Example:\n\n\tIP_ADDRESS=192.0.2.5 NETMASK=255.255.255.0 DOMAIN=example.net\n\n')
            sys.exit(-1)

        if options.force:
            tpl.set_variables(tplvars, unset='UNSET')
        else:
            unsetvars = tpl.set_variables(tplvars)

            if len(unsetvars) > 0:
                cli.list_unset_vars(unsetvars)
                sys.exit(-1)

        print(tpl.render_template())

        sys.exit(0)

    # Interactive mode: Prompt user for variables
    else:
        # Useless, but every good program needs a 'force' parameter
        if options.force:
            tpl.set_variables({}, unset='UNSET')
        else:
            tplvars = cli.interactive_mode(tpl)
            tpl.set_variables(tplvars)

        print(tpl.render_template())

        sys.exit(0)
dataset/ETHPy150Open verbosemode/confplate/confplate/confplate.py/main/original