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 …
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.
So far your programs asked for data with input() . But many real tools ( git , pip , python itself) are used from the terminal …
With sys.argv you have to check lengths, convert types and write the error messages yourself. The argparse module of the standard…
Besides positional arguments, almost all tools accept options : named arguments that start with - or -- and are optional (unless…
Tools like git group different actions into subcommands : git commit , git push , git log . Each one has its own arguments…
A twenty-line script can be written straight through. A real program needs order : small functions, an entry function and, when…
Create a Python program that reads its command-line arguments with sys.argv from the sys module. The arguments of each test are…
Create a Python program that receives any number of numbers as arguments and shows a summary. The arguments of each test are…
Create a command-line calculator with the argparse module, which validates the arguments for you. The arguments of each test are…
Create a program that imitates the Unix wc command: it counts lines, words and characters of the text it receives through…
Create a program with argparse that shows the multiplication table of a number, with options to choose the range. The arguments…
Create a program with argparse that computes statistics of a list of numbers passed as arguments. The arguments of each test are…
Create a command-line tool with subcommands , like git does. Each subcommand is a different action with its own arguments. The…
Command-line programs report whether they went well with their exit code : 0 means success and any other value, error. Create a…
Create a length unit converter with argparse , using required options with a set of allowed values. The arguments of each test…
Bring together everything you have learned in a well-structured program: small functions, a main function that receives the…