Argparse: Python command line parser

tisto 3,534 views 9 slides Jun 18, 2009
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Barcelona Python Meetup


Slide Content

Argparse: Python command line parser
Timo Stollenwerk
April 27th, 2009
Timo Stollenwerk Argparse: Python command line parser

Introduction
makes writing command line tools in Python easy
just briey describe your command line interface
argparse will take care of the rest, including:
parsing the arguments and ags from sys.argv
converting arg strings into objects for your program
formatting and printing any help messages
and much more ...
Timo Stollenwerk Argparse: Python command line parser

Optparse vs. Argparse
handling positional arguments
supporting sub-commands
allowing alternative option prexes like + and /
handling zero-or-more and one-or-more style arguments
producing more informative usage messages
providing a much simpler interface for custom types and
actions
Timo Stollenwerk Argparse: Python command line parser

Installation
e a s y _ i n s t a l l a r g p a r s e
Timo Stollenwerk Argparse: Python command line parser

ArgumentParser
>>> import a r g p a r s e
>>> p a r s e r = a r g p a r s e . ArgumentParser (
. . . d e s c r i p t i o n ='A foo that bars ' )
>>> p a r s e r . p r i n t _ h e l p ( )
usage : a r g p a r s e . py [h ]
A foo t hat bars
o p t i o n a l arguments :
h , h e l p show t h i s h e l p message and e x i t
Timo Stollenwerk Argparse: Python command line parser

The add_argument() method
optional argument (myscript.py -f)
>>> p a r s e r . add_argument('f ' , ' foo ' )
positional argument (myscript.py foo bar)
>>> p a r s e r . add_argument ( ' arg1 ' )
>>> p a r s e r . add_argument ( ' arg2 ' )
Timo Stollenwerk Argparse: Python command line parser

The parse_args() method
myscript.py -a foo -s
>>> p a r s e r . add_argument('a ' , ' add ' ,
. . . a c t i o n ='append ' , nargs =1)
>>> p a r s e r . add_argument('s ' , ' show ' ,
. . . a c t i o n =' store_true ' )
>>> a r g s = p a r s e r . parse_args ( )
>>> p r i n t a r g s . add
foo
>>> p r i n t a r g s . show
True
Timo Stollenwerk Argparse: Python command line parser

A more complex example
sum.py 2 3
p a r s e r = a r g p a r s e . ArgumentParser (
d e s c r i p t i o n ='Sum the i n t e g e r s . ' )
p a r s e r . add_argument (
' i n t e g e r s ' , metavar =' i n t ' , type=i n t , nargs ='+',
h e l p ='one of the i n t e g e r s to be summed ' )
p a r s e r . add_argument (
' log ' , type=a r g p a r s e . FileType ( 'w' ) , d e f a u l t=s y s . stdout ,
h e l p =' the f i l e where the sum should be w r i t t e n '
'( d e f a u l t : w r i t e the sum to s t d o u t ) ' )
a r g s = p a r s e r . parse_args ( )
a r g s . l o g . w r i t e ('% s \n ' % sum( a r g s . i n t e g e r s ) )
a r g s . l o g . c l o s e ( )
Timo Stollenwerk Argparse: Python command line parser

Futher Information
http://argparse.googlecode.com
Timo Stollenwerk Argparse: Python command line parser