Re: How does one make argparse print usage when no options areprovided on the command line?

Discussion in 'Python' started by rh, Dec 6, 2012.

  1. rh

    rh Guest

    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:

    You did this:
    if len(sys.argv) == 1:

    The other reply did this:
    if len(sys.argv) <= 1:

    Is there a favorite or preferred way to do it?


    >
    > before I call parser.parse_args()
    >
    > Should work
    >
    >
    > --
    > Bruno Dupuis



    --
    rh, Dec 6, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page