Next / Previous / Contents / TCC Help System / NM Tech homepage

28.10.2. Overall flow of argument processing

Here is the general procedure for using the argparse module to check and process your command line arguments, which in most cases will come from sys.argv (see Section 28.6, “sys: Universal system interface”).

  1. Create an argparse.ArgumentParser instance. See Section 28.10.3, “The ArgumentParser() constructor”.

  2. For each command line argument, call the .add_argument() method of that instance once to define what that argument may be. See Section 28.10.4, “The ArgumentParser.add_argument() method”.

  3. Call the .parse_args() method of the instance. If there are any errors in the command line, this method will print a message summarizing the arguments, and then terminate execution.

    If the arguments are correct according to the rules that you have specified, all the values will be packaged into an object containing all your arguments as attributes, with default values filled in where necessary. See Section 28.10.5, “The ArgumentParser.parse_args() method”.