Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python argparse to handle arbitrary numeric options (like HEAD(1))

Is there a way to trick argparse into accepting arbitrary numeric arguments like HEAD(1)?

head -5 test.txt

is equivalent to

head -n 5 test.txt

My current approach is to use parse_known_args() and then handle the remainder, but I'd wish there was something a tad more elegant.

like image 521
Manuel Ebert Avatar asked Apr 05 '12 10:04

Manuel Ebert


People also ask

How do you add an optional argument in Argparse?

To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.

What does Nargs do in Argparse?

The add_argument() method action - The basic type of action to be taken when this argument is encountered at the command line. nargs - The number of command-line arguments that should be consumed. const - A constant value required by some action and nargs selections.


1 Answers

Neither agrparse nor optparse supports this.

like image 80
Maksym Polshcha Avatar answered Oct 21 '22 05:10

Maksym Polshcha