Skip to content

aNdr3W03/Python-Argument-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Python Argument Parser

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.

Script Description

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:

  1. Make sure Python is installed on your local machine (python --version)
    If Python has not been installed, you can download it via this link
  2. Save or Download the Python file on local as argParser.py via this link
  3. Run the file on CLI or Terminal or CMD
  4. See the output example below if you need help

Output Example

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

References

(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

(Programiz) Python strptime()

(Dicoding) Penanganan Pengecualian

(Dicoding) Library Populer pada Python

Releases

No releases published

Packages

No packages published