How does one make argparse print usage when no options are provided on the command line?

Ian Kelly ian.g.kelly at gmail.com
Wed Dec 5 19:31:57 CET 2012


On Wed, Dec 5, 2012 at 9:48 AM, rh <richard_hubbe11 at lavabit.com> 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.

if len(sys.argv) <= 1:
    parser.print_usage()
    sys.exit(1)
else:
    args = parser.parse_args()



More information about the Python-list mailing list