Command line applications (CLI)
- These applications process the commands and arguments
# windows
ping google.com
# linux
ping -c 4 google.com
- Any command line application will generally have arguments
- Arguments are of two types
- positional arguments: position matters
- keyword arguments: this argument has a key and a value
- short form
-<char>: Examples are-a test -c 4 - long form
--<work>: Examples are--anchor test --count 4
- short form
- Arguments can be defined as
- optional: where if the user does not pass a value we assume default
- required: where the user has to pass and argument
Simple CLI Based Application in python
-
Topic: Standard Library
-
Lets build a cli application which takes two arguments and display the sum of two numbers, lets call this add
-
in the world of python module refers to the python file and the name of module
filename without extension
add.py => add
add_test.py => add_test
- Reusable prompt
You are an expert in python.
I will be passing a standard libraries name
I want you to summarize the purpose of this standard library
Some examples of what we can do this standard library.
- Create a file called as add.py with the following code
"""
This module is a cli application which adds two numbers
"""
# import a standard module
import sys
print(sys.argv)
if __name__ == "__main__":
number_1 = int(sys.argv[1])
number_2 = int(sys.argv[2])
result = number_1 + number_2
print(f"result is: {result}")
-
Now navigate to the terminal and execute the following
python add.py 1 3 -
CLI Applications generally need to validate the inputs i.e. arguments passed
-
To simplify the cli application building we have two possible options
-
To summarize we need to
- Process arguments
- Validations
- Help/Documentation for cli
-
Exercise: Build a cli using argparse to convert kilos into pounds
python convert.py 13
`13 kilos => 28.6601 pounds`
Financial calculator
-
Lets build a financial calculator which helps in
- EMI calculators
- Intrest Calculators
-
The core idea is to learn and apply
- python standard libraries
- Object oriented concepts
- Design patterns
- Best Practices
