Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'args': 0.04; 'interpreter': 0.04; 'sys': 0.05; 'exception.': 0.07; 'executed': 0.07; 'subject:How': 0.09; 'python': 0.09; '(it': 0.09; 'interpreter,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:command': 0.09; 'terry': 0.09; 'dec': 0.15; 'file,': 0.15; '.py': 0.16; 'argparse': 0.16; 'doc)': 0.16; 'pathname': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'subject: \n ': 0.16; 'subject:argparse': 0.16; 'subject:options': 0.16; 'subject:usage': 0.16; 'subject:when': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'copied': 0.17; 'script.': 0.17; 'creates': 0.18; 'examples': 0.18; 'jan': 0.18; '>>>': 0.18; 'bit': 0.21; 'import': 0.21; 'object.': 0.22; "i'd": 0.22; 'dependent': 0.23; 'this:': 0.23; 'command': 0.24; 'script': 0.24; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'possibility': 0.27; 'separate': 0.27; '+0100': 0.27; 'options': 0.27; 'header:X -Complaints-To:1': 0.28; 'noticed': 0.28; 'code': 0.31; 'not.': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'pm,': 0.35; 'too.': 0.35; 'subject:?': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'wanted': 0.36; "didn't": 0.36; 'method': 0.36; 'operating': 0.36; 'does': 0.37; 'option': 0.37; 'being': 0.37; 'passed': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'apply': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'within': 0.64; 'reply': 0.66; '(according': 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: How does one make argparse print usage when no options are provided on the command line? Date: Thu, 06 Dec 2012 00:06:29 -0500 References: <20121205084830.74e842119d151781db385406@lavabit.com> <20121205174237.GH10865@hud> <20121205164851.93f151641a1d085e5262f59d@lavabit.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <20121205164851.93f151641a1d085e5262f59d@lavabit.com> 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354770413 news.xs4all.nl 6941 [2001:888:2000:d::a6]:45762 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34361 On 12/5/2012 7:48 PM, rh wrote: > On Wed, 5 Dec 2012 18:42:37 +0100 > Bruno Dupuis wrote: > >> On Wed, Dec 05, 2012 at 08:48:30AM -0800, rh wrote: >>> I have argparse working with one exception. I wanted the program to >>> print out usage when no command line options are given. But I only >>> came across other examples where people didn't use argparse but >>> instead printed out a separate usage statement. So they used >>> argparse for everything but the case where no command line args are >>> given. >>> >> >> this is quite raw, but i'd add >> >> import sys >> if len(sys.argv) == 1: >> sys.argv.append('-h') > > This works too. I guess I like the print_usage() method better. > > Being new to python I have noticed that I had copied a bit of code that did > > if len(sys.argv[1:]) == 0: This needlessly creates and tosses a new object. > > You did this: > if len(sys.argv) == 1: This does not. > The other reply did this: > if len(sys.argv) <= 1: This allows for the possibility that len(sys.argv) == 0. However, that can (according to the doc) only happen when starting the interpreter interactively without a script. Since that does not apply to code within a .py file, I prefer == 1. "argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string." -- Terry Jan Reedy