CLI Calculator
In this project, you'll learn to create a tool that can be used from a command line interface (CLI). First, you'll see how you can directly pass Python code from the command line and create bash
shortcuts to simplify the invocation. Second, you'll see how to use Python features to create a custom CLI application. 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 OS. Usepy
instead ofpython3.9
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 command line typing
- Evaluate string content as Python code
- Create user friendly command line interfaces
- Allow
stdin
as source of 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
- 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.