Getopt multiple argument syntaxes
<p>So I'm working on an assignment in a python class that I'm taking, but have gotten stuck with something I can't really find any further information about (neither on SO, Google or in the courseware). </p>

<p>I need help with how to handle arguments with multiple types of syntaxes - like  <strong>[arg] and &lt; arg ></strong>, which is something I've been unable to find any further information about.</p>

<p>Here is an example use-case that SHOULD work.</p>

<pre><code>&gt;&gt;&gt; ./marvin-cli.py --output=&lt;filename.txt&gt; ping &lt;http://google.com&gt;
&gt;&gt;&gt; Syntax error near unexpected token 'newline'
</code></pre>

<p>The below code works fine for any use-case where I haven't defined any further output than writing to the console:</p>

<pre><code># Switch through all options
try:

    opts, args = getopt.getopt(sys.argv[1:], "hsv", ["help","version","silent", "get=", "ping=", "verbose", "input=", "json"])
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            printUsage(EXIT_SUCCESS)
        elif opt in ("-s", "--silent"):
            VERBOSE = False 
        elif opt in ("--verbose"):
            VERBOSE = True 
        elif opt in ("--ping"):
            ping(arg)
        elif opt in ("--input"):
            print("Printing to: ", arg)
        else:
            assert False, "Unhandled option"


except Exception as err:
    print("Error " ,err)
    print(MSG_USAGE)
    # Prints the callstack, good for debugging, comment out for production
    #traceback.print_exception(Exception, err, None)
    sys.exit(EXIT_USAGE)
#print(sys.argv[1])
</code></pre>

<p>Example usage:</p>

<pre><code>&gt;&gt;&gt; ./marvin-cli.py ping http://google.com
&gt;&gt;&gt; Latency 100ms
</code></pre>

<p>And this is a snippet showing how the ping works:</p>

<pre><code>def ping(URL):
    #Getting necessary imports
    import requests
    import time

    #Setting up variables
    start = time.time()
    req = requests.head(URL)
    end = time.time()

    #printing result
    if VERBOSE == False:
        print("I'm pinging: ", URL)
        print("Received HTTP response (status code): ", req.status_code)

    print("Latency: {}ms".format(round((end - start) * 1000, 2)))
</code></pre>

<p><code>[]</code> and <code>&lt;&gt;</code> are commonly used to visually indicate option requirement. Typically <code>[xxxx]</code> means the option or argument is optional and <code>&lt;xxxx&gt;</code> required.</p>

<p>The sample code you provide handles option flags, but not the required arguments. Code below should get you started in the right direction.</p>

<pre><code>try:
    opts, args = getopt.getopt(sys.argv[1:], "hsv", ["help", "version", "silent", "verbose", "output=", "json"])
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            printUsage(EXIT_SUCCESS)
        elif opt in ("-s", "--silent"):
            VERBOSE = False 
        elif opt in ("--verbose"):
            VERBOSE = True
        elif opt in ("--output"):
            OUTPUTTO = arg
            print("Printing to: ", arg)
        else:
            assert False, "Unhandled option"

    assert len(args) &gt; 0, "Invalid command usage"
    # is there a "&lt;command&gt;" function defined?
    assert args[0] in globals(), "Invalid command {}".format(args[0])

    # pop first argument as the function to call
    command = args.pop(0)
    # pass args list to function
    globals()[command](args)


def ping(args):
    #Getting necessary imports
    import requests
    import time

    # validate arguments
    assert len(args) != 1, "Invalid argument to ping"
    URL = args[0]

    #Setting up variables
    start = time.time()
    req = requests.head(URL)
    end = time.time()

    #printing result
    if VERBOSE == False:
        print("I'm pinging: ", URL)
        print("Received HTTP response (status code): ", req.status_code)

    print("Latency: {}ms".format(round((end - start) * 1000, 2)))
</code></pre>

python python-2.7 python-3.x getopt