Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: CSCI 195
Views: 5926
Image: ubuntu2004
1
from argparse import ArgumentParser
2
3
# Create an ArgumentParser
4
parser = ArgumentParser()
5
6
# Configure the ArgumentParser with 3 different arguments
7
parser.add_argument('--name', help='Your full name', required=True)
8
parser.add_argument('--age', help='Your age in years', type=int)
9
parser.add_argument('--birthday', help='Your birth month and year (e.g. Apr 7)')
10
11
# Ask the ArgumentParser object to look at the command line arguments that were entered by the user
12
arguments = parser.parse_args()
13
14
# Greet the user. We don't need to check whether the user entered a value for --name
15
# because we set it to required, and the program won't even get here if we don't
16
print ("Hello {}".format(arguments.name))
17
18
# Check to see if they entered an age
19
if arguments.age:
20
print ('You are {} years old'.format(arguments.age))
21
22
if arguments.birthday:
23
print ('You will be {} next {}'.format(arguments.age+1, arguments.birthday))
24
else:
25
print ('You will be {} on your next birthday'.format(arguments.age+1))
26