#!/usr/bin/env python

# The MIT License

# Copyright (c) 2012 ObjectLabs Corporation

# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

__author__ = 'abdul'

import dargparse
import sys
import traceback
import datetime

from dargparse import dargparse
from datetime import  datetime
# Dargparse is the future of command line parsers

# Define your parse dictionary
HELLO_PARSER ={
    "prog": "hello-dargparse",
    "description" : "This is hello world dargparse example",
    "args": [
        {
            "name": "yourName",
            "type" : "positional",
            "help": "Your name",
            "displayName" : "<YOUR NAME>",
            "nargs": 1
        },
        {
            "name": "printDate",
            "type" : "optional",
            "help": "print the current date",
            "cmd_arg": [
                "-d",
                "--printDate"
            ],
            "nargs": 0,
            "action": "store_true",
            "default": False
        },
        {
            "name": "repeat",
            "type" : "optional",
            "help": "repeat for N times",
            "cmd_arg": [
                "-r",
                "--repeat"
            ],
            "nargs": 1,
            "default": 1,
            "valueType": int,
        }

    ]


}



###############################################################################
# MAIN
###############################################################################
def main(args):
# build the parse object
    helloDargParser = dargparse.build_parser(HELLO_PARSER)

    # parse the command line

    parsed_options = helloDargParser.parse_args(args)
    now = datetime.now()
    if parsed_options.printDate:
        msg = "Hello %s! the time now is %s" % (parsed_options.yourName,now)
    else:
        msg = "Hello %s!" % (parsed_options.yourName)
    print '\n'.join([msg] * parsed_options.repeat)


###############################################################################
########################                   ####################################
########################     BOOTSTRAP     ####################################
########################                   ####################################
###############################################################################

if __name__ == '__main__':
    try:
        # call main with a sub-list starting skipping the
        # "hello-dargparse" command itself (which is the first arg)
        main(sys.argv[1:])
    except (SystemExit, KeyboardInterrupt) , e:
        if e.code == 0:
            pass
        else:
            raise
    except:
        traceback.print_exc()
