
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:Falseunless you type it.default=is used when the option is left out.type=intturns the typed text"5"into the number5.
You also get --help for free!
Assignment
Fill in the ___ blanks so that build_parser() returns a parser
where:
folderis a required positional argument.--applyis a switch (Falseunless given).--outdefaults to"report.txt".--limitis converted to anintand defaults to0.- At the bottom, call the parser's method that parses the list.
Fill in the ___ blanks so that build_parser() returns a parser
where:
folderis a required positional argument.--applyis a switch (Falseunless given).--outdefaults to"report.txt".--limitis converted to anintand defaults to0.- At the bottom, call the parser's method that parses the list.
main.py Python sleeping
Press Run to try your code, then Check to test it.