Skip to content

Instantly share code, notes, and snippets.

@graipher
Last active December 6, 2016 09:14
Show Gist options
  • Save graipher/687417aa3eec2d2a6f19a54253f4ff09 to your computer and use it in GitHub Desktop.
Save graipher/687417aa3eec2d2a6f19a54253f4ff09 to your computer and use it in GitHub Desktop.
Argparse vs docopt
"""SMTP user checker.
Usage:
argparser_comp.py SERVER USERNAMES [(-p <p>| --port=<p>)]
argparser_comp.py (-h | --help)
Options:
-h --help Show this screen.
-p <p> --port=<p> SMTP port of server [default: 25].
"""
import os.path
import argparse
from docopt import docopt
def is_file(f):
if os.path.isfile(f):
return f
raise OSError("{} does not exist".format(f))
def parse_args():
parser = argparse.ArgumentParser(description="SMTP user checker")
parser.add_argument('server', help="IP address of server")
parser.add_argument('usernames', type=is_file,
help="Path to list of user names")
parser.add_argument('--port', '-p', type=int,
default=25, help="SMTP port of server")
return parser.parse_args()
def parse_args2():
args = docopt(__doc__)
return argparse.Namespace(server=args["SERVER"],
usernames=is_file(args["USERNAMES"]),
port=int(args["--port"]))
if __name__ == "__main__":
args1 = parse_args()
args2 = parse_args2()
assert args1 == args2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment