CLI Calculator
In this project, you'll learn to write a tool that can be used from a command line interface (CLI). First, you'll see how to directly pass Python code from the command line and create shell shortcuts to simplify the invocation. Next, you'll learn how to create a custom CLI application with Python. Finally, you'll be given exercises to test your understanding and resource links for further exploration. The links to these sections are given below:
![]()
If you are on Windows, you can still follow along most of this project by skipping the
bash
specific portions. The CLI tool creation usingargparse
isn't tied to a specific operating system. Usepy
instead ofpython3.13
for program execution. See docs.python: Windows command-line and the rest of that page for more details. Alternatively, you can use Windows Subsystem for Linux.
Project summary
- Execute Python instructions from the command line
- Use shell shortcuts to simplify typing on the command line
- Evaluate string content as Python code
- Create user friendly command line interfaces
- Allow
stdin
as a source for user input
The following modules and concepts will be utilized in this project:
- docs.python: sys
- docs.python: argparse
- docs.python: eval
- docs.python: Modules
- docs.python: Exception handling
Real world influence
I had two main reasons to implement this project:
- learn how to write a CLI application with Python
- a simple CLI calculator for personal use
There are powerful tools like bc but I wanted easier syntax without fiddling with settings like scale
. Instead of writing a shell script to customize bc
for my use cases, I went with Python since I wanted to learn about the argparse
module too.