This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Introduction to Command Line Interface

In this module, we will look at how you can implement simple Command Line Interfaces (CLI) in Python.

Command Line Interfaces (CLI) are text-based user interfaces (UI) when you type in commands to get the computer to do something. Think of computer geeks from the 80s.

Computer Geek

Contrast this to Graphical User Interfaces (GUI). where you run your programs with your mouse or by tapping your screen.

I believe you have been using quite a lot of CLIs in your degree so far. For example:

  • git commit -m "When will this coursework end?"
  • cd courseworks
  • ssh why.cant.i@just-work-locally.ic.ac.uk
  • ls -la
  • python3 more_coursework.py

So far, we have only been allowing your Python programs to simply run as a script: python3 too_many_courseworks.py.

With our good old guessing game, we have requested for users to input their guess only after running the program. We could have instead asked the user to enter their guess straightaway when running the program, for example:

$ python3 guessing_game.py 42

You guessed right! It's 42!

Or maybe we can allow users to make 5 guesses immediately and hope one of them is correct, like this:

$ python3 guessing_game.py 80 27 42 13 65

Great guess! 42 is the answer!

How could this be useful? Well you can for example write a simple calculator program on Python

$ python3 calculator.py 3 5 add 

8

Or you might want to run a program with some parameters that a user provides

$ python3 audio_player.py --user josiah123 --music 80s

Welcome back to the best audio player in the world, josiah123!
You said you feel like listening to something from the 80s? Here is what we recommend!
- Rick Atsley: Never Gonna Give You Up
- Bonnie Tyler: Total Eclipse of the Heart
- Foreigners: I Want to Know What Love is
- Cyndi Lauper: Time after Time
- Michael Jackson: Billie Jean

In this module, we will look at how we can read in these user parameters and process them with Python.