argparse: python command line parser

9

Upload: plone-foundation

Post on 08-May-2015

6.796 views

Category:

Technology


0 download

DESCRIPTION

Barcelona Python Meetup

TRANSCRIPT

Page 1: Argparse: Python command line parser

Argparse: Python command line parser

Timo Stollenwerk

April 27th, 2009

Timo Stollenwerk Argparse: Python command line parser

Page 2: Argparse: Python command line parser

Introduction

makes writing command line tools in Python easy

just brie�y 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

Page 3: Argparse: Python command line parser

Optparse vs. Argparse

handling positional arguments

supporting sub-commands

allowing alternative option pre�xes 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 andactions

Timo Stollenwerk Argparse: Python command line parser

Page 4: 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

Page 5: Argparse: Python command line parser

ArgumentParser

>>> impor t a r g p a r s e>>> pa r s e r = a r gp a r s e . ArgumentParser (. . . d e s c r i p t i o n ='A foo tha t bars ' )>>> pa r s e r . p r i n t_he l p ( )usage : a r g p a r s e . py [−h ]

A foo tha t ba r s

o p t i o n a l arguments :−h , −−he l p show t h i s h e l p message and e x i t

Timo Stollenwerk Argparse: Python command line parser

Page 6: Argparse: Python command line parser

The add_argument() method

optional argument (myscript.py -f)

>>> pa r s e r . add_argument ( '− f ' , '−− foo ' )

positional argument (myscript.py foo bar)

>>> pa r s e r . add_argument ( ' arg1 ' )>>> pa r s e r . add_argument ( ' arg2 ' )

Timo Stollenwerk Argparse: Python command line parser

Page 7: Argparse: Python command line parser

The parse_args() method

myscript.py -a foo -s

>>> pa r s e r . add_argument ( '−a ' , '−−add ' ,. . . a c t i o n ='append ' , na rg s=1)>>> pa r s e r . add_argument ( '− s ' , '−−show ' ,. . . a c t i o n =' s to r e_t rue ' )>>> arg s = p a r s e r . pa r se_args ( )>>> p r i n t a r g s . addfoo>>> p r i n t a r g s . showTrue

Timo Stollenwerk Argparse: Python command line parser

Page 8: Argparse: Python command line parser

A more complex example

sum.py 2 3

p a r s e r = a r gp 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 ' , t ype=in t , na rg s = '+ ' ,h e l p='one o f the i n t e g e r s to be summed ' )

p a r s e r . add_argument ('−− l og ' , t ype=a r gp a r s e . F i l eType ( 'w' ) , d e f a u l t=s y s . s tdout ,h e l p=' the f i l e where the sum shou l d 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 dou t ) ' )

a r g s = p a r s e r . pa r se_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

Page 9: Argparse: Python command line parser

Futher Information

http://argparse.googlecode.com

Timo Stollenwerk Argparse: Python command line parser