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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 12/11/2013 5:26 AM, Ben Finney wrote: > Better design is to make the argument list a parameter to the =E2=80=98= main=E2=80=99 > function; this allows constructing an argument list specially for > calling that function, without =E2=80=98main=E2=80=99 needing to know t= he difference. > > You'll also want to catch SystemExit and return that as the =E2=80=98ma= in=E2=80=99 > function's return value, to make it easier to use as a function when > that's needed. > > def main(argv=3DNone): > if argv is None: > argv =3D sys.argv > > exit_code =3D 0 > try: > command_name =3D argv[0] > config =3D parse_command_args(argv[1:]) > do_whatever_this_program_does(config) > except SystemExit, exc: > exit_code =3D exc.code > > return exit_code > > if __name__ =3D=3D "__main__": > import sys > exit_code =3D main(sys.argv) > sys.exit(exit_code) > > That way, the normal behaviour is to use the =E2=80=98sys.argv=E2=80=99= list and to > raise SystemExit (via =E2=80=98sys.exit=E2=80=99) to exit the program. = But =E2=80=98main=E2=80=99 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. --=20 Terry Jan Reedy