This is an assignment practice for the Getting Started Programming with Python (Memulai Pemrograman dengan Python) class, Popular Libraries in Python (Library Populer pada Python) module, on the Machine Learning Developer Learning Path at Dicoding Indonesia.
The argument parser is useful when creating small program or script that can be directly receive parameter(s) when the program is run. Application program or script can be run via the CLI (Command-Line Interface) or Terminal or CMD (Command Prompt).
When the program is run, it will aks for parameter name
and date of birth
. If the program is run without the parameters or only one parameter, then the program will return an error. The date of birth parameter format is DD-MM-YYYY
. If the format of the date of birth parameter is incorrect, then the program will hanlde an exception (ValueError
) which is checked in the validDate
function.
The program will calculate the age that obtained from the date of birth parameter, so if the age is less than 30 years, then the output will display "kakak", otherwise the output will display "bapak."
You can try this program by:
argParser.py -h
usage: argParser.py [-h] -n NAME -d DOB
options:
-h, --help show this help message and exit
-n NAME, --name NAME input your name
-d DOB, --dob DOB input your date of birth (DD-MM-YYYY)
argParser.py -n INPUTNAME -d INPUTDOB
INPUTNAME : input your name
INPUTDOB : input your date of birth (format: DD-MM-YYYY)
argParser.py -n Andrew -d 20-01-2003
Terima kasih telah menggunakan argParser.py pada tahun 2022, kakak Andrew
argParser.py -n Andrew -d 20-01-1990
Terima kasih telah menggunakan argParser.py pada tahun 2022, bapak Andrew
argParser.py -n Andrew -d 2003-01-20
usage: argParser.py [-h] -n NAME -d DOB
argParser.py: error: argument -d/--dob: ERROR, not a valid date: '2003-01-20'
argParser.py -n Andrew
usage: argParser.py [-h] -n NAME -d DOB
argParser.py: error: the following arguments are required: -d/--dob
argParser.py -d 2003-01-20
usage: argParser.py [-h] -n NAME -d DOB
argParser.py: error: the following arguments are required: -n/--name
argParser.py
usage: argParser.py [-h] -n NAME -d DOB
argParser.py: error: the following arguments are required: -n/--name, -d/--dob
(Stack Overflow) Specify date format for Python argparse input arguments
(Stack Overflow) Python : extract parameters created with argparse
(Stack Overflow) Age from birthdate in python
(Stack Overflow) TypeError:'datetime.datetime' object is not subscriptable