Skip to content

Instantly share code, notes, and snippets.

@bala-codes
Last active July 20, 2020 04:49
Show Gist options
  • Save bala-codes/303f63b6eb149a172c8dd9365d7a24ae to your computer and use it in GitHub Desktop.
Save bala-codes/303f63b6eb149a172c8dd9365d7a24ae to your computer and use it in GitHub Desktop.
# Simple Demonstration of Python Argparser in command line arguments
# Simple Demonstration of Python Argparser in command line arguments# Simple Demonstration of Python Argparser in command line arguments
import argparse
from distutils.util import strtobool
def add_and_display(args):
print(args)
print()
if args.permissions :
if args.operation == "add":
print("Adding two numbers :", args.number_1 + args.number_2)
else:
print("Subract two numbers :", args.number_1 - args.number_2)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Trying out some argparsers.')
parser.add_argument('--permissions', type=strtobool, default=False, required = True,
help="Whether to display the output")
parser.add_argument("--operation", type=str, required=True, choices=["add", "subtract"],
help="Operation to be performed")
parser.add_argument("--number_1", type=int, default=0, required=True,
help="Placeholder for the first number")
parser.add_argument("--number_2", type=int, default=0, required=False,
help="Placeholder for the second number")
# Below code aggregates the data specified in the previous placeholders
args = parser.parse_args()
add_and_display(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment