argparse

Why use argparse rather than optparse?

I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse. Why has yet another command-line parsing module been created? Why should I use it instead of optparse? Are their new features I should know about? ...

Setting separate choices for argument with two values using argparse in Python

I currently have the following code: import argparse parser = argparse.ArgumentParser(description='Adds a new modem to Iridium account') parser.add_argument('imei', metavar='I', nargs=1, help='the modems IMEI') parser.add_argument('-t1', '--type1', metavar='t1', nargs=1, choices=('email', 'directip', 'sbddevice'), default='directip', h...

Adding options which both change behaviour and store an argument

Using the argparse module, is it possible to perform multiple actions for a given argument? Specifically, I'd like to provide a -l/--list option with nargs='?' that will change the behaviour of the program from its main function to one of giving information about all possibilities in a set or about one particular possibility. Normally ...

Using the argparse output to call functions.

Currently my code looks like this. It allows me to parse multiple parameters my program script gets. Is there a different way that is closer to 'best practices'? I haven't seen code actually using the output of argparse, only how to set it up. def useArguments(): x = 0 while x <= 5: if x == 0: ...

python argparse: How can I display help automatically on error?

Currently when I enter invalid options or omit positional arguments, argparse kicks me back to the prompt and displays the usage for my app. This is ok, but I would rather automatically display the full help listing (that explains the options, etc) than require the user to type ./myscript.py -h Thanks! Jamie ...

Python argparse: How to insert newline the help text?

I'm using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g. from argparse import ArgumentParser parser = ArgumentParser(description='test') parser.add_argument('-g', choices=['a', 'b', 'g', 'd', 'e'], default='a', help="Some option, where\n" ...

Display help message with python argparse when script is called without any arguments

This might be a simple one. Assume I have a program that uses argparse to process command line arguments/options. The following will print the 'help' message: ./myprogram -h or: ./myprogram --help But, if I run the script without any arguments whatsoever, it doesn't do anything. What I want it to do is to display the usage message ...