optional python arguments without dashes but with additional parameters?
Question: what I'd like to do in Python is accept arguments of the following format:

    
    
    script.py START | STOP | STATUS | MOVEABS <x> <y> | MOVEREL <x> <y>
    

So in other words,

  1. I don't want to deal with hyphens;
  2. I have multiple possibilities, ONE of which is required;
  3. Each is mutually exclusive;
  4. Some of the commands (E.G. moveabs and moverel) have additional required arguments, but these args and should not be present with any other argument.

Can this be done in python and would I use argparse or something else? Thanks.


Answer: Using `docopt` you can do this quite easily.

# Install `docopt` first:

    
    
    $ pip install docopt
    

# Write the `script.py`:

    
    
    """
    Usage:
        script.py (start | stop | status | moveabs <x> <y> | moverel <x> <y>)
    """
    from docopt import docopt
    
    if __name__ == "__main__":
        args = docopt(__doc__)
        print args
    

# and run it:

first showing basic help:

    
    
    $ python script.py
    Usage:
        script.py (start | stop | status | moveabs <x> <y> | moverel <x> <y>)
    

then try subcommands:

start

    
    
    $ python script.py start
    {'<x>': None,
     '<y>': None,
     'moveabs': False,
     'moverel': False,
     'start': True,
     'status': False,
     'stop': False}
    

stop

    
    
    $ python script.py stop
    {'<x>': None,
     '<y>': None,
     'moveabs': False,
     'moverel': False,
     'start': False,
     'status': False,
     'stop': True}
    

moveabs

    
    
    $ python script.py moveabs 11 22
    {'<x>': '11',
     '<y>': '22',
     'moveabs': True,
     'moverel': False,
     'start': False,
     'status': False,
     'stop': False}
    

moverel

    
    
    $ python script.py moverel 11 22
    {'<x>': '11',
     '<y>': '22',
     'moveabs': False,
     'moverel': True,
     'start': False,
     'status': False,
     'stop': False}
    

