15
Lesson 41 of 44

Command-line programs

Learn to create programs that are used from the terminal, like professional tools. In this lesson you will use sys.argv and standard input, output and error, the argparse module (arguments, options, flags and subcommands), exit codes and the structure of a program with main, and you will see how to organize a project in packages with pyproject.toml, pip and virtual environments. You will apply it in calculators, wc-like counters, validators and converters.

  • 5 theory articles
  • 10 practice exercises
Start lesson

Fundamentals concepts 5

Command-line programs and sys.argv

So far your programs asked for data with input() . But many real tools ( git , pip , python itself) are used from the terminal …

Theory
Read more

argparse: positional arguments

With sys.argv you have to check lengths, convert types and write the error messages yourself. The argparse module of the standard…

Theory
Read more

Options and flags

Besides positional arguments, almost all tools accept options : named arguments that start with - or -- and are optional (unless…

Theory
Read more

Subcommands and exit codes

Tools like git group different actions into subcommands : git commit , git push , git log . Each one has its own arguments…

Theory
Read more

Structuring a program and creating packages

A twenty-line script can be written straight through. A real program needs order : small functions, an entry function and, when…

Theory
Read more

Practice exercises 10

Greeting with arguments

Create a Python program that reads its command-line arguments with sys.argv from the sys module. The arguments of each test are…

Exercise Easy
Solve it

Sum of the arguments

Create a Python program that receives any number of numbers as arguments and shows a summary. The arguments of each test are…

Exercise Easy
Solve it

Calculator with argparse

Create a command-line calculator with the argparse module, which validates the arguments for you. The arguments of each test are…

Exercise Medium
Solve it

Counter like wc

Create a program that imitates the Unix wc command: it counts lines, words and characters of the text it receives through…

Exercise Medium
Solve it

Multiplication table with options

Create a program with argparse that shows the multiplication table of a number, with options to choose the range. The arguments…

Exercise Medium
Solve it

Statistics of a list of numbers

Create a program with argparse that computes statistics of a list of numbers passed as arguments. The arguments of each test are…

Exercise Medium
Solve it

Text tool with subcommands

Create a command-line tool with subcommands , like git does. Each subcommand is a different action with its own arguments. The…

Exercise Hard
Solve it

Validator with exit code

Command-line programs report whether they went well with their exit code : 0 means success and any other value, error. Create a…

Exercise Hard
Solve it

Unit converter

Create a length unit converter with argparse , using required options with a set of allowed values. The arguments of each test…

Exercise Hard
Solve it

Complete program with main

Bring together everything you have learned in a well-structured program: small functions, a main function that receives the…

Exercise Hard
Solve it