Skip to content

Instantly share code, notes, and snippets.

@JSONOrona
Last active January 4, 2021 16:37
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save JSONOrona/9276216 to your computer and use it in GitHub Desktop.
Save JSONOrona/9276216 to your computer and use it in GitHub Desktop.
Python command line argument example using argparse module
#!/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
@alaudet
Copy link

alaudet commented Jan 27, 2016

I have been reading up quite a bit on argparse for command line arguments. Lots of reading but very few practical examples. This one is excellent, thanks.

@AbdulRehmanJanjua-InfoSec

thanks it was life saver

Copy link

ghost commented Jun 30, 2017

Very Helpful. Thanks.

@VMuliadi
Copy link

Excellent tutorial. Thanks for sharing.

@jasoriya
Copy link

At last a practical implementation. Thanks!

@maba18
Copy link

maba18 commented Jan 19, 2018

I always come back to this example whenever I create a new python project. Thanks.

@adambot806
Copy link

good job

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment