Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34417
| From | rh <richard_hubbe11@lavabit.com> |
|---|---|
| Subject | Re: How does one make argparse print usage when no options are provided on the command line? |
| Date | 2012-12-06 10:42 -0800 |
| References | <20121205084830.74e842119d151781db385406@lavabit.com> <20121205174237.GH10865@hud> <20121205164851.93f151641a1d085e5262f59d@lavabit.com> <k9p94t$8af$1@ger.gmane.org> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.581.1354819354.29569.python-list@python.org> (permalink) |
On Thu, 06 Dec 2012 00:06:29 -0500
Terry Reedy <tjreedy@udel.edu> wrote:
> On 12/5/2012 7:48 PM, rh wrote:
> > On Wed, 5 Dec 2012 18:42:37 +0100
> > Bruno Dupuis <python.ml.bruno.dupuis@lisael.org> 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."
Interesting, I thought maybe <= 1 was to handle some strange case
where sys.argv < 0. And I guess I stick with == 1. Always good to know
from the get go what's the best way to do the basics.
>
> --
> Terry Jan Reedy
>
--
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: How does one make argparse print usage when no options are provided on the command line? rh <richard_hubbe11@lavabit.com> - 2012-12-06 10:42 -0800
csiph-web