Loading topic
argparse sits on top of a small set of operating-system conventions. A program gets a list of argument strings, three standard data streams and an integer exit status. argparse automates the first of these and helps with the third. If you know all three, pa6 min readsys.argv, standard output, standard error and exit codes are, from the previous chapter. Here you meet the two objects at the center of argparse: the `A5 min readargparse handles arguments identified by their position. It assumes you can write a function, run a script from a shell, and have read the previous chapter on creating a parser and calling parse_args.5 min readArgumentParser and call parse_args, and that you have seen a positional argument. It covers how optional arguments are declared and parsed, how their names turn into attributes, and the edge cases that cause confusing errors.5 min readadd_argument, and read the resulting Namespace. You should also know that an option's dest is the attribute name on that namespace. Here you will learn what the action argument does and how to pick the right one.5 min readadd_argument, read the resulting Namespace, and run a script from a shell. You should also know what an exit code is. Here you will learn how argparse turns raw text into Python values and how to reject bad input with clear messages.4 min readparse_args. Here you will control how many values an argument takes, what it holds when the user leaves it out, and when leaving it out is an error.5 min readmain(argv) pattern from earlier chapters. It covers how argparse builds --help output, how to control each part of it, and how to check that it works.5 min readArgumentParser, add_argument and parse_args from earlier chapters. Here the focus moves from individual arguments to the shape of the whole program.5 min readadd_argument, that you know what main(argv) is for, and that you can read a Python traceback. Here you decide what the program does when something goes wrong. That means where the message goes, what status the process returns, and which failures g5 min readmain(argv) returns an exit code. It covers two tools for larger interfaces. Argument groups organize the help output. Mutually exclusive groups reject invalid combinations of options.5 min readgit commit, pip install and docker run use subcommands. One program offers several actions, and each action has its own arguments. This chapter assumes you can build a parser with positional arguments, options and flags, and that you have a main(argv) function that returns an e4 min readargparse parser, that you structure programs around a main(argv) function that returns an exit code, and that you have used pytest at a basic level (test functions, assert, fixtures). It uses the greet tool from the earlier chapters.5 min readpython greet.py Ada. Users expect to type greet Ada from any directory. This chapter shows how to make that work with a pyproject.toml entry point, what the installer generates, and which mistakes cause most failures.6 min readadd_argument, you know what store_true does, and you have written a tool with subcommands using add_subparsers and set_defaults(func=...). Parent parsers solve one problem: several parsers need the same options, and copying the add_argument 5 min readArgumentParser, options and positional arguments, subcommands, a main(argv) function, and installing a tool through [project.scripts]. The goal is to give you criteria for choosing a library. It does not te5 min read