SlideShare uma empresa Scribd logo
1 de 9
Baixar para ler offline
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




   easy_install argparse




                 Timo Stollenwerk   Argparse: Python command line parser
ArgumentParser




   i m p o r t a r g p a r s e
   p a r s e r = a r g p a r s e . A r g u m e n t P a r s e r (
   ...        d e s c r i p t i o n ='A f o o t h a t b a r s ' )
   p a r s e r . p r i n t _ h e l p ( )
  u s a g e : a r g p a r s e . py [ − h ]

  A foo that bars

  o p t i o n a l a rg um en ts :
    −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 ' , n a r g s =1)
   p a r s e r . add_argument ( ' − s ' , '−− show ' ,
   ...        a c t i o n =' s t o r e _ t r u e ' )
   a r g s = p a r s e r . p a r s e _ a r g s ( )
   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 t h e 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 y p e=i n t , n a r g s = '+ ' ,
      h e l p =' one o f t h e i n t e g e r s t o be summed ' )
  p a r s e r . add_argument (
     '−− l o g ' , t y p e=a r g p a r s e . F i l e T y p e ( 'w ' ) , d e f a u l t=s y s .
      h e l p =' t h e f i l e where t h e sum s h o u l d be w r i t t e n '
                 ' ( d e f a u l t : w r i t e t h e sum t o s t d o u t ) ' )

  args = parser . 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 ) )
  args . log . close ()
                           Timo Stollenwerk   Argparse: Python command line parser
Futher Information




      http://argparse.googlecode.com




                     Timo Stollenwerk   Argparse: Python command line parser

Mais conteúdo relacionado

Mais procurados

PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MoreMatt Harrison
 
Conditionalstatement
ConditionalstatementConditionalstatement
ConditionalstatementRaginiJain21
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandasPiyush rai
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programmingSrinivas Narasegouda
 
File handling in Python
File handling in PythonFile handling in Python
File handling in PythonMegha V
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementAnil Bapat
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionarySoba Arjun
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Alexander Hendorf
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)Thai Pangsakulyanont
 

Mais procurados (20)

PyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and MorePyCon 2013 : Scripting to PyPi to GitHub and More
PyCon 2013 : Scripting to PyPi to GitHub and More
 
Conditionalstatement
ConditionalstatementConditionalstatement
Conditionalstatement
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
File handling & regular expressions in python programming
File handling & regular expressions in python programmingFile handling & regular expressions in python programming
File handling & regular expressions in python programming
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Python : Regular expressions
Python : Regular expressionsPython : Regular expressions
Python : Regular expressions
 
Python recursion
Python recursionPython recursion
Python recursion
 
Python strings
Python stringsPython strings
Python strings
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]Introduction to Pandas and Time Series Analysis [PyCon DE]
Introduction to Pandas and Time Series Analysis [PyCon DE]
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 

Destaque

Proyecto Rescate de los valores.
Proyecto Rescate de los valores.Proyecto Rescate de los valores.
Proyecto Rescate de los valores.pablovalerocpe
 
Festival de coplas de sexto
Festival de coplas de sextoFestival de coplas de sexto
Festival de coplas de sextoLogos Academy
 
Poemas y canciones para sexto C
Poemas y canciones para sexto CPoemas y canciones para sexto C
Poemas y canciones para sexto CRomysand
 
Retahilas por mayra rodriguez
Retahilas  por mayra rodriguezRetahilas  por mayra rodriguez
Retahilas por mayra rodriguezmayra_rodriguez
 
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridos
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridosCopilacion de refranes, calavera lírica, adivinanzas, copla y corridos
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridosAlejandra Araujo
 
Cartelera informativa terra hidro
Cartelera informativa terra hidroCartelera informativa terra hidro
Cartelera informativa terra hidroandre15vacas
 
Invitación navideña
Invitación navideñaInvitación navideña
Invitación navideñaandre15vacas
 
Folleto terrahidro
Folleto terrahidroFolleto terrahidro
Folleto terrahidroandre15vacas
 
De sabios, de poetas y de locos...
De sabios, de poetas y de locos...De sabios, de poetas y de locos...
De sabios, de poetas y de locos...ali16enero
 
8.9.inventario de fuentes en el blog
8.9.inventario de fuentes en el blog8.9.inventario de fuentes en el blog
8.9.inventario de fuentes en el blogGUALUPITAPLCS
 
Cartilla talleres socializacion Bunny Bonita
Cartilla talleres socializacion Bunny BonitaCartilla talleres socializacion Bunny Bonita
Cartilla talleres socializacion Bunny Bonitanachoxavy
 
Formas literarias
Formas literariasFormas literarias
Formas literariasMaryAcostaA
 
Retahilas infantiles
Retahilas infantilesRetahilas infantiles
Retahilas infantilespurifran
 

Destaque (20)

Proyecto Rescate de los valores.
Proyecto Rescate de los valores.Proyecto Rescate de los valores.
Proyecto Rescate de los valores.
 
Festival de coplas de sexto
Festival de coplas de sextoFestival de coplas de sexto
Festival de coplas de sexto
 
Coplas sobre los apodos
Coplas sobre los   apodosCoplas sobre los   apodos
Coplas sobre los apodos
 
Poemas y canciones para sexto C
Poemas y canciones para sexto CPoemas y canciones para sexto C
Poemas y canciones para sexto C
 
Retahilas por mayra rodriguez
Retahilas  por mayra rodriguezRetahilas  por mayra rodriguez
Retahilas por mayra rodriguez
 
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridos
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridosCopilacion de refranes, calavera lírica, adivinanzas, copla y corridos
Copilacion de refranes, calavera lírica, adivinanzas, copla y corridos
 
Cartelera informativa terra hidro
Cartelera informativa terra hidroCartelera informativa terra hidro
Cartelera informativa terra hidro
 
Invitación navideña
Invitación navideñaInvitación navideña
Invitación navideña
 
Folleto terrahidro
Folleto terrahidroFolleto terrahidro
Folleto terrahidro
 
De sabios, de poetas y de locos...
De sabios, de poetas y de locos...De sabios, de poetas y de locos...
De sabios, de poetas y de locos...
 
Chile
ChileChile
Chile
 
8.9.inventario de fuentes en el blog
8.9.inventario de fuentes en el blog8.9.inventario de fuentes en el blog
8.9.inventario de fuentes en el blog
 
Adivinanzas
AdivinanzasAdivinanzas
Adivinanzas
 
Cartilla talleres socializacion Bunny Bonita
Cartilla talleres socializacion Bunny BonitaCartilla talleres socializacion Bunny Bonita
Cartilla talleres socializacion Bunny Bonita
 
Formas literarias
Formas literariasFormas literarias
Formas literarias
 
Retahilas infantiles
Retahilas infantilesRetahilas infantiles
Retahilas infantiles
 
Cartilla
CartillaCartilla
Cartilla
 
Folklore
FolkloreFolklore
Folklore
 
Folclor
FolclorFolclor
Folclor
 
Maria dolores coplas retahilas amorfinos
Maria dolores coplas retahilas amorfinosMaria dolores coplas retahilas amorfinos
Maria dolores coplas retahilas amorfinos
 

Semelhante a Argparse: Python command line parser

The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python scriptsaniac
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on SessionDharmesh Tank
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Bryan O'Sullivan
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for ScientistsAndreas Dewes
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smileMartin Melin
 
Pattern matching
Pattern matchingPattern matching
Pattern matchingshravs_188
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Parkpointstechgeeks
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting StartedKent Ohashi
 
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet Pôle Systematic Paris-Region
 
Functional Thinking for Java Developers (presented in Javafest Bengaluru)
Functional Thinking for Java Developers (presented in Javafest Bengaluru)Functional Thinking for Java Developers (presented in Javafest Bengaluru)
Functional Thinking for Java Developers (presented in Javafest Bengaluru)KonfHubTechConferenc
 
pa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processingpa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text ProcessingRodrigo Senra
 
ZCA: A component architecture for Python
ZCA: A component architecture for PythonZCA: A component architecture for Python
ZCA: A component architecture for PythonTimo Stollenwerk
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?UFPA
 

Semelhante a Argparse: Python command line parser (20)

The bones of a nice Python script
The bones of a nice Python scriptThe bones of a nice Python script
The bones of a nice Python script
 
Ch2
Ch2Ch2
Ch2
 
Day3
Day3Day3
Day3
 
Basic of Python- Hands on Session
Basic of Python- Hands on SessionBasic of Python- Hands on Session
Basic of Python- Hands on Session
 
Real World Haskell: Lecture 7
Real World Haskell: Lecture 7Real World Haskell: Lecture 7
Real World Haskell: Lecture 7
 
Howto argparse
Howto argparseHowto argparse
Howto argparse
 
Qt Translations
Qt TranslationsQt Translations
Qt Translations
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for Scientists
 
Command line arguments that make you smile
Command line arguments that make you smileCommand line arguments that make you smile
Command line arguments that make you smile
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Python - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave ParkPython - Getting to the Essence - Points.com - Dave Park
Python - Getting to the Essence - Points.com - Dave Park
 
Free Monads Getting Started
Free Monads Getting StartedFree Monads Getting Started
Free Monads Getting Started
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
PyData Paris 2015 - Track 3.2 Serge Guelton et Pierrick Brunet
 
Functional Thinking for Java Developers (presented in Javafest Bengaluru)
Functional Thinking for Java Developers (presented in Javafest Bengaluru)Functional Thinking for Java Developers (presented in Javafest Bengaluru)
Functional Thinking for Java Developers (presented in Javafest Bengaluru)
 
pa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processingpa-pe-pi-po-pure Python Text Processing
pa-pe-pi-po-pure Python Text Processing
 
week-12x
week-12xweek-12x
week-12x
 
ZCA: A component architecture for Python
ZCA: A component architecture for PythonZCA: A component architecture for Python
ZCA: A component architecture for Python
 
Diving deep into twig
Diving deep into twigDiving deep into twig
Diving deep into twig
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 

Mais de Timo Stollenwerk

German Aerospace Center (DLR) Web Relaunch
German Aerospace Center (DLR) Web RelaunchGerman Aerospace Center (DLR) Web Relaunch
German Aerospace Center (DLR) Web RelaunchTimo Stollenwerk
 
Performance Testing (Python Barcamp Cologne 2020)
Performance Testing (Python Barcamp Cologne 2020)Performance Testing (Python Barcamp Cologne 2020)
Performance Testing (Python Barcamp Cologne 2020)Timo Stollenwerk
 
Roadmap to a Headless Plone
Roadmap to a Headless PloneRoadmap to a Headless Plone
Roadmap to a Headless PloneTimo Stollenwerk
 
Plone.restapi - a bridge to the modern web
Plone.restapi - a bridge to the modern webPlone.restapi - a bridge to the modern web
Plone.restapi - a bridge to the modern webTimo Stollenwerk
 
The Butler and The Snake (Europython 2015)
The Butler and The Snake (Europython 2015)The Butler and The Snake (Europython 2015)
The Butler and The Snake (Europython 2015)Timo Stollenwerk
 
Hypermedia APIs mit Javascript und Python
Hypermedia APIs mit Javascript und PythonHypermedia APIs mit Javascript und Python
Hypermedia APIs mit Javascript und PythonTimo Stollenwerk
 
Plone Testing & Continuous Integration Team Report 2014
Plone Testing & Continuous Integration Team Report 2014Plone Testing & Continuous Integration Team Report 2014
Plone Testing & Continuous Integration Team Report 2014Timo Stollenwerk
 
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...The Beauty and the Beast - Modern Javascript Development with AngularJS and P...
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...Timo Stollenwerk
 
The Butler and the Snake - JCICPH
The Butler and the Snake - JCICPHThe Butler and the Snake - JCICPH
The Butler and the Snake - JCICPHTimo Stollenwerk
 
The Butler and the Snake - Continuous Integration for Python
The Butler and the Snake - Continuous Integration for PythonThe Butler and the Snake - Continuous Integration for Python
The Butler and the Snake - Continuous Integration for PythonTimo Stollenwerk
 
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...Timo Stollenwerk
 
Who let the robot out? - Building high quality software with Continuous Integ...
Who let the robot out? - Building high quality software with Continuous Integ...Who let the robot out? - Building high quality software with Continuous Integ...
Who let the robot out? - Building high quality software with Continuous Integ...Timo Stollenwerk
 
The Future Is Written - Building next generation Plone sites with plone.app.c...
The Future Is Written - Building next generation Plone sites with plone.app.c...The Future Is Written - Building next generation Plone sites with plone.app.c...
The Future Is Written - Building next generation Plone sites with plone.app.c...Timo Stollenwerk
 
Einführung Test-driven Development
Einführung Test-driven DevelopmentEinführung Test-driven Development
Einführung Test-driven DevelopmentTimo Stollenwerk
 

Mais de Timo Stollenwerk (20)

German Aerospace Center (DLR) Web Relaunch
German Aerospace Center (DLR) Web RelaunchGerman Aerospace Center (DLR) Web Relaunch
German Aerospace Center (DLR) Web Relaunch
 
Performance Testing (Python Barcamp Cologne 2020)
Performance Testing (Python Barcamp Cologne 2020)Performance Testing (Python Barcamp Cologne 2020)
Performance Testing (Python Barcamp Cologne 2020)
 
Python & JavaScript
Python & JavaScriptPython & JavaScript
Python & JavaScript
 
Roadmap to a Headless Plone
Roadmap to a Headless PloneRoadmap to a Headless Plone
Roadmap to a Headless Plone
 
Plone.restapi - a bridge to the modern web
Plone.restapi - a bridge to the modern webPlone.restapi - a bridge to the modern web
Plone.restapi - a bridge to the modern web
 
Divide et impera
Divide et imperaDivide et impera
Divide et impera
 
The Butler and The Snake (Europython 2015)
The Butler and The Snake (Europython 2015)The Butler and The Snake (Europython 2015)
The Butler and The Snake (Europython 2015)
 
Hypermedia APIs mit Javascript und Python
Hypermedia APIs mit Javascript und PythonHypermedia APIs mit Javascript und Python
Hypermedia APIs mit Javascript und Python
 
Plone Testing & Continuous Integration Team Report 2014
Plone Testing & Continuous Integration Team Report 2014Plone Testing & Continuous Integration Team Report 2014
Plone Testing & Continuous Integration Team Report 2014
 
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...The Beauty and the Beast - Modern Javascript Development with AngularJS and P...
The Beauty and the Beast - Modern Javascript Development with AngularJS and P...
 
The Butler and the Snake - JCICPH
The Butler and the Snake - JCICPHThe Butler and the Snake - JCICPH
The Butler and the Snake - JCICPH
 
The Butler and the Snake - Continuous Integration for Python
The Butler and the Snake - Continuous Integration for PythonThe Butler and the Snake - Continuous Integration for Python
The Butler and the Snake - Continuous Integration for Python
 
AngularJS & Plone
AngularJS & PloneAngularJS & Plone
AngularJS & Plone
 
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...
Who let the robot out? Qualitativ hochwertige Software durch Continuous Integ...
 
Plone5
Plone5Plone5
Plone5
 
Who let the robot out? - Building high quality software with Continuous Integ...
Who let the robot out? - Building high quality software with Continuous Integ...Who let the robot out? - Building high quality software with Continuous Integ...
Who let the robot out? - Building high quality software with Continuous Integ...
 
The Future Is Written - Building next generation Plone sites with plone.app.c...
The Future Is Written - Building next generation Plone sites with plone.app.c...The Future Is Written - Building next generation Plone sites with plone.app.c...
The Future Is Written - Building next generation Plone sites with plone.app.c...
 
Plone Einführung
Plone EinführungPlone Einführung
Plone Einführung
 
Einführung Test-driven Development
Einführung Test-driven DevelopmentEinführung Test-driven Development
Einführung Test-driven Development
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 

Último

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Último (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Argparse: Python command line parser

  • 1. Argparse: Python command line parser Timo Stollenwerk April 27th, 2009 Timo Stollenwerk Argparse: Python command line parser
  • 2. 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
  • 3. 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
  • 4. Installation easy_install argparse Timo Stollenwerk Argparse: Python command line parser
  • 5. ArgumentParser i m p o r t a r g p a r s e p a r s e r = a r g p a r s e . A r g u m e n t P a r s e r ( ... d e s c r i p t i o n ='A f o o t h a t b a r s ' ) p a r s e r . p r i n t _ h e l p ( ) u s a g e : a r g p a r s e . py [ − h ] A foo that bars o p t i o n a l a rg um en ts : −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
  • 6. 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
  • 7. 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 ' , n a r g s =1) p a r s e r . add_argument ( ' − s ' , '−− show ' , ... a c t i o n =' s t o r e _ t r u e ' ) a r g s = p a r s e r . p a r s e _ a r g s ( ) 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
  • 8. 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 t h e 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 y p e=i n t , n a r g s = '+ ' , h e l p =' one o f t h e i n t e g e r s t o be summed ' ) p a r s e r . add_argument ( '−− l o g ' , t y p e=a r g p a r s e . F i l e T y p e ( 'w ' ) , d e f a u l t=s y s . h e l p =' t h e f i l e where t h e sum s h o u l d be w r i t t e n ' ' ( d e f a u l t : w r i t e t h e sum t o s t d o u t ) ' ) args = parser . 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 ) ) args . log . close () Timo Stollenwerk Argparse: Python command line parser
  • 9. Futher Information http://argparse.googlecode.com Timo Stollenwerk Argparse: Python command line parser