liveBook · Manning

--num

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

--num is a book-specific long command-line option that specifies the number of verses to generate.

--num is a long command-line option for choosing a quantity used by a program. It accepts an integer and supplies a program-specific default when omitted. section 11.1 section 13.0

The option’s meaning and validation depend on the program:

  • bottles.py defaults to 10 verses, rejects non-integers automatically through argparse, and rejects values below 1 with parser.error(). section 11.1
  • twelve_days.py defaults to 12 days and restricts the value to the range 1–12 before generating verses. section 13.1
  • wod.py defaults to four exercises and passes the value as the sample size for random.sample(). section 19.1 section 19.1.5
  • password.py uses --num for the number of passwords, while --num_words separately controls words per password. section 20.1
$ ./bottles.py -h
usage: bottles.py [-h] [-n number]

Bottles of beer song

optional arguments:
 -h, --help            show this help message and exit
 -n number, --num number
                       How many bottles (default: 10)

The help output shows the short alias -n, integer-valued argument, and default of 10 for bottles.py. section 11.1

$ ./twelve_days.py -h
usage: twelve_days.py [-h] [-n days] [-o FILE]

Twelve Days of Christmas

optional arguments:
 -h, --help            show this help message and exit
 -n days, --num days   Number of days to sing (default: 12)
 -o FILE, --outfile FILE
                       Outfile (default: <_io.TextIOWrapper name='<stdout>'
                       mode='w' encoding='utf-8'>)

The twelve_days.py help output shows --num alongside --outfile and documents its default of 12. section 13.1

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Defines integer defaults, argparse conversion, and program-specific bounds, but does not descend into argparse internals.
Examples
Demonstrates bottles of beer, Twelve Days of Christmas, workout selection, and password generation.
What you can do after
Operate/configure command-line programs by supplying valid quantities and interpreting argparse errors for invalid values.
How it connects
Connects command-line arguments with argparse, parser.error(), range(), random.sample(), and output-generation functions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.