Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61620

Re: Is there any advantage to using a main() in python scripts?

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Is there any advantage to using a main() in python scripts?
Date 2013-12-11 16:22 -0500
References <32615c9a-b983-4399-bb55-6df6c230f247@googlegroups.com> <7wsitzlm3o.fsf@benfinney.id.au>
Newsgroups comp.lang.python
Message-ID <mailman.3934.1386796935.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/11/2013 5:26 AM, Ben Finney wrote:

> Better design is to make the argument list a parameter to the ‘main’
> function; this allows constructing an argument list specially for
> calling that function, without ‘main’ needing to know the difference.
>
> You'll also want to catch SystemExit and return that as the ‘main’
> function's return value, to make it easier to use as a function when
> that's needed.
>
>      def main(argv=None):
>          if argv is None:
>              argv = sys.argv
>
>          exit_code = 0
>          try:
>              command_name = argv[0]
>              config = parse_command_args(argv[1:])
>              do_whatever_this_program_does(config)
>          except SystemExit, exc:
>              exit_code = exc.code
>
>          return exit_code
>
>      if __name__ == "__main__":
>          import sys
>          exit_code = main(sys.argv)
>          sys.exit(exit_code)
>
> That way, the normal behaviour is to use the ‘sys.argv’ list and to
> raise SystemExit (via ‘sys.exit’) to exit the program. But ‘main’ itself
> can, without any further changes, be called as a function that receives
> the command line as a parameter, and returns the exit code.

In particular, it is easier to write tests when argv is a parameter.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Is there any advantage to using a main() in python scripts? JL <lightaiyee@gmail.com> - 2013-12-11 01:55 -0800
  Re: Is there any advantage to using a main() in python scripts? Ben Finney <ben+python@benfinney.id.au> - 2013-12-11 21:26 +1100
  Re: Is there any advantage to using a main() in python scripts? Chris Angelico <rosuav@gmail.com> - 2013-12-11 21:41 +1100
  Re: Is there any advantage to using a main() in python scripts? marduk@letterboxes.org - 2013-12-11 07:57 -0500
    Re: Is there any advantage to using a main() in python scripts? Roy Smith <roy@panix.com> - 2013-12-11 09:20 -0500
  Re: Is there any advantage to using a main() in python scripts? Roy Smith <roy@panix.com> - 2013-12-11 09:17 -0500
    Re: Is there any advantage to using a main() in python scripts? rusi <rustompmody@gmail.com> - 2013-12-11 06:24 -0800
  Re: Is there any advantage to using a main() in python scripts? bob gailer <bgailer@gmail.com> - 2013-12-11 10:42 -0500
  Re: Is there any advantage to using a main() in python scripts? Chris Angelico <rosuav@gmail.com> - 2013-12-12 02:49 +1100
  Re: Is there any advantage to using a main() in python scripts? Terry Reedy <tjreedy@udel.edu> - 2013-12-11 16:22 -0500

csiph-web