Last active
November 3, 2022 23:00
-
-
Save AtmaMani/4cdda473f02d70ed26035a3f191d5b73 to your computer and use it in GitHub Desktop.
Command line arguments in Python
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
## just a file to try out command line parsing | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('url', help="Enter portal url of the form: \ | |
https://portalname.domain.com/webadaptor") | |
#Create optional args by prefixing it with -- | |
#Create short names for same by prefixing it with - | |
#Specify default values using 'default' argument | |
parser.add_argument('-u','--username', help="Enter admin username", default='admin') | |
parser.add_argument('-p','--password', help="Enter admin password") | |
#Argparase treats all inputs as strings, so specify int as type for numerical inputs | |
parser.add_argument('-n','--num_users', help="Enter number of users", type=int) | |
#Create bool parameters by specifying 'action' | |
parser.add_argument('-b','--publish', help="Publish content for each user", action='store_true') | |
# Perform action | |
args = parser.parse_args() | |
print("Signing into : " + args.url) | |
print("Signing in using: " + args.username + " into " + args.url) | |
if args.num_users: | |
print("Creating {} users".format(str(args.num_users))) | |
if args.publish: | |
print("Publishing content for users") |
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
""" =============================== output ======================================= | |
E:\code\tools>python command_line_args.py | |
usage: command_line_args.py [-h] [-u USERNAME] [-p PASSWORD] [-n NUM_USERS] | |
[-b] | |
url | |
command_line_args.py: error: the following arguments are required: url | |
----------------------------------------------------------------------------------- | |
E:\code\geosaurus\geosaurus_scripts\tools>python command_line_args.py -h | |
usage: command_line_args.py [-h] [-u USERNAME] [-p PASSWORD] [-n NUM_USERS] | |
[-b] | |
url | |
positional arguments: | |
url Enter portal url of the form: | |
https://portalname.domain.com/webadaptor | |
optional arguments: | |
-h, --help show this help message and exit | |
-u USERNAME, --username USERNAME | |
Enter admin username | |
-p PASSWORD, --password PASSWORD | |
Enter admin password | |
-n NUM_USERS, --num_users NUM_USERS | |
Enter number of users | |
-b, --publish Publish content for each user | |
----------------------------------------------------------------------------------- | |
E:\code\>python command_line_args.py http://ags.com -u admin -p passw -n 10 -b | |
Signing into : http://ags.com | |
Signing in using: admin into http://ags.com | |
Creating 10 users | |
Publishing content for users | |
----------------------------------------------------------------------------------- | |
E:\code\>python command_line_args.py http://ags.com -u admin3 -p passw | |
Signing into : http://ags.com | |
Signing in using: admin3 into http://ags.com | |
----------------------------------------------------------------------------------- | |
E:\code\>python command_line_args.py http://ags.com | |
Signing into : http://ags.com | |
Signing in using: admin into http://ags.com | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment