-
-
Save jorgeas80/71757c0995b91ef99ee3391bf5166401 to your computer and use it in GitHub Desktop.
Python command line argument example using argparse module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
''' Python command line argument example using argparse module | |
Example output: | |
./parser.py --server=pyserver --port=8080,443,25,22,21 --keyword=pyisgood | |
Server name: [ pyserver ] | |
Port: [ 8080 ] | |
Port: [ 443 ] | |
Port: [ 25 ] | |
Port: [ 22 ] | |
Port: [ 21 ] | |
Keyword assigned: [ pyisgood ] | |
''' | |
import argparse | |
__author__ = 'Jason Vasquez Orona' | |
def get_args(): | |
'''This function parses and return arguments passed in''' | |
# Assign description to the help doc | |
parser = argparse.ArgumentParser( | |
description='Script retrieves schedules from a given server') | |
# Add arguments | |
parser.add_argument( | |
'-s', '--server', type=str, help='Server name', required=True) | |
parser.add_argument( | |
'-p', '--port', type=str, help='Port number', required=True, nargs='+') | |
parser.add_argument( | |
'-k', '--keyword', type=str, help='Keyword search', required=False, default=None) | |
# Array for all arguments passed to script | |
args = parser.parse_args() | |
# Assign args to variables | |
server = args.server | |
port = args.port[0].split(",") | |
keyword = args.keyword | |
# Return all variable values | |
return server, port, keyword | |
# Run get_args() | |
# get_args() | |
# Match return values from get_arguments() | |
# and assign to their respective variables | |
server, port, keyword = get_args() | |
# Print the values | |
print "\nServer name: [ %s ]\n" % server | |
for p in port: | |
print "Port: [ %s ]" % p | |
print "\nKeyword assigned: [ %s ]\n" % keyword |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment