Fill in

Commands for the Tool

A tool earns trust when its safe choice is the default. Pip's tidy-up tool only previews, unless you add --apply.

On a real computer you'd run it from a terminal:

python tidy.py inbox --apply --out report.txt

Python puts those words in the list sys.argv, and parser.parse_args() reads them for you. PyQuest has no terminal, so we hand the list over ourselves: parser.parse_args(["inbox", "--apply"]). Same parser, same result.

parser.add_argument("folder")             # required
parser.add_argument("--apply", action="store_true")
parser.add_argument("--out", default="report.txt")
parser.add_argument("--limit", type=int, default=0)
  • A name without dashes is positional: it must be given.
  • action="store_true" makes a switch: False unless you type it.
  • default= is used when the option is left out.
  • type=int turns the typed text "5" into the number 5.

You also get --help for free!

Assignment

Fill in the ___ blanks so that build_parser() returns a parser where:

  1. folder is a required positional argument.
  2. --apply is a switch (False unless given).
  3. --out defaults to "report.txt".
  4. --limit is converted to an int and defaults to 0.
  5. At the bottom, call the parser's method that parses the list.