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?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'receives': 0.04; 'value,': 0.04; 'argument': 0.05; 'none:': 0.07; 'sys': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'exit': 0.09; 'function,': 0.09; 'parameter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'try:': 0.09; 'def': 0.12; 'jan': 0.12; '(via': 0.16; 'finney': 0.16; "function's": 0.16; 'function;': 0.16; 'parameter.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'systemexit': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'command': 0.22; 'import': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'config': 0.24; 'changes,': 0.26; 'specially': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'raise': 0.29; 'needed.': 0.30; 'easier': 0.31; 'allows': 0.31; 'except': 0.35; 'but': 0.35; 'subject:?': 0.36; 'list': 0.37; 'ben': 0.38; 'to:addr:python- list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'called': 0.40; 'catch': 0.60; 'received:173': 0.61; 'further': 0.61; "you'll": 0.62; 'needing': 0.65; 'skip:\xe2 10': 0.65; '8bit%:40': 0.68; 'subject:there': 0.68; '8bit%:43': 0.74; 'difference.': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Is there any advantage to using a main() in python scripts?
Date Wed, 11 Dec 2013 16:22:00 -0500
References <32615c9a-b983-4399-bb55-6df6c230f247@googlegroups.com> <7wsitzlm3o.fsf@benfinney.id.au>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <7wsitzlm3o.fsf@benfinney.id.au>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3934.1386796935.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386796935 news.xs4all.nl 2843 [2001:888:2000:d::a6]:43721
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61620

Show key headers only | 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