SlideShare a Scribd company logo
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
Dizionario Italiano a cura di Enrico Olivetti
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
PyCon 23: Smettiamola di scrivere script
CLI Applications
What's wrong with scripts?
Why CLI Applications?
1. Argparse
2. Click
3. Docopt
print("This is important code")
1
2
for index, arg in enumerate(sys.argv):
3
print(f"[{index}]: {arg}")
4
What's Wrong with Scripts?
Easy to get started
Limited possibilities for structure
Hard to (unit) test
No dependency management
Deployment may require care
Custom user experience
print("Usage: foo <bar> --baz")
def main():
1
2
3
if __name__ == "__main__":
4
main()
5
Why CLI Applications?
Standardized user experience
More possibilities for structure
Possibilities for all kinds of testing
Dependency management
Packaging & distribution
import argparse
from . import __version__
def parse_arguments():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--version', action='version',
version=__version__)
parser.add_argument('filename')
args = parser.parse_args()
return args
def main():
args = parse_arguments()
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Argparse
import click
@click.command()
@click.version_option()
@click.argument('filename')
def main(filename):
click.echo(filename)
1
2
3
4
5
6
7
Click
import click
@click.command()
@click.version_option()
@click.argument('filename', type=click.Path(exists=True))
def main(filename):
click.echo(filename)
1
2
3
4
5
6
7
import click
@click.command()
@click.version_option()
@click.argument('infile', type=click.File())
def main(infile):
click.echo(infile.read())
1
2
3
4
5
6
7
"""Foobar
Usage:
foobar (-h | --help | --version)
foobar [-s | --silent] <file>
foobar [-v | --verbose] <file>
Positional arguments:
file target file path name
Optional arguments:
-h, --help show this help message and exit
-s, --silent don't show progress output
-v, --verbose explain progress verbosely
--version show program's version number and exit
"""
from docopt import docopt
from . import __version__
def parse_arguments():
args = docopt(__doc__, version=__version__)
return dict(
file=args['<file>'],
silent=args['-s'] or args['--silent'],
verbose=args['-v'] or args['--verbose'],
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Docopt
$ copier gh:painless-software/python-cli-test-helpers pycon-italia-cli
🎤 engine
(Use arrow keys)
» Argparse
Click
Docopt
...
Demo
🐍 ✨
✨
https://python-cli-test-helpers.readthedocs.io
Thank you!
for your precious time
Painless Software
Less pain, more fun.
Most images taken from Wikipedia (CC-SA)
Alberto Sordi animated GIF stolen from giffetteria.it

More Related Content

PDF
Python Summit 2022: Never Write Scripts Again
PDF
How to write maintainable code without tests
PPT
Python advanced 3.the python std lib by example – application building blocks
PPT
Lpi lição 01 exam 102 objectives
PPTX
PVS-Studio and static code analysis technique
PDF
Debugging Python with gdb
PPT
Dotnetintroduce 100324201546-phpapp02
PDF
Inria Tech Talk : Comment améliorer la qualité de vos logiciels avec STAMP
Python Summit 2022: Never Write Scripts Again
How to write maintainable code without tests
Python advanced 3.the python std lib by example – application building blocks
Lpi lição 01 exam 102 objectives
PVS-Studio and static code analysis technique
Debugging Python with gdb
Dotnetintroduce 100324201546-phpapp02
Inria Tech Talk : Comment améliorer la qualité de vos logiciels avec STAMP

Similar to PyCon 23: Smettiamola di scrivere script (20)

PDF
External Language Stored Procedures for MySQL
PDF
Re-Design with Elixir/OTP
PDF
The Ring programming language version 1.3 book - Part 60 of 88
PPTX
Mock cli with Python unittest
PPTX
Shell Script Tutorial
PDF
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
PPTX
Improving Code Quality Through Effective Review Process
PPTX
.net Framework
PDF
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
PDF
Suite Script 2.0 API Basics
PDF
TypeScript for Java Developers
PDF
Whoops! Where did my architecture go?
PDF
How to Design a Great API (using flask) [ploneconf2017]
PDF
Ice mini guide
PPTX
C programming language tutorial
PPT
PDF
ScalaUA - distage: Staged Dependency Injection
PDF
Reproducibility with R
PDF
Kostis Sagonas: Cool Tools for Modern Erlang Program Developmen
PDF
Divide and Conquer – Microservices with Node.js
External Language Stored Procedures for MySQL
Re-Design with Elixir/OTP
The Ring programming language version 1.3 book - Part 60 of 88
Mock cli with Python unittest
Shell Script Tutorial
distage: Purely Functional Staged Dependency Injection; bonus: Faking Kind Po...
Improving Code Quality Through Effective Review Process
.net Framework
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
Suite Script 2.0 API Basics
TypeScript for Java Developers
Whoops! Where did my architecture go?
How to Design a Great API (using flask) [ploneconf2017]
Ice mini guide
C programming language tutorial
ScalaUA - distage: Staged Dependency Injection
Reproducibility with R
Kostis Sagonas: Cool Tools for Modern Erlang Program Developmen
Divide and Conquer – Microservices with Node.js
Ad

More from Peter Bittner (15)

PDF
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
PDF
PyCon 24: Pythonic Deployment - Juju and Charms
PDF
APPUiO Quick Start (OpenShift > DevOps > App Dev)
PDF
Pee Dee Kay (PDK) - Puppet Development Kit
PDF
Managing 100+ WAFs
PDF
EuroPython 2019: Modern Continuous Delivery for Python Developers
PDF
Avoid the Vendor Lock-in Trap (with App Deployment)
PDF
A guide to modern software development 2018
PDF
PyCon 9: Continuous Delivery starts at your Development Dnvironment
PDF
Painless Continuous Delivery – DjangoCon 2017
PDF
Continuous Delivery for Python Developers – PyCon Otto
PDF
Fix-Price Projects And Agile – PyCon Sette
PDF
Creating a Collaboration Platform (Leveraging the Django Eco System)
PDF
Linux für Einsteiger und UmsteigerInnen (Vortrag)
PDF
Linux auf meinem PC
Nix(OS) for Python Developers - PyCon 25 (Bologna, Italia)
PyCon 24: Pythonic Deployment - Juju and Charms
APPUiO Quick Start (OpenShift > DevOps > App Dev)
Pee Dee Kay (PDK) - Puppet Development Kit
Managing 100+ WAFs
EuroPython 2019: Modern Continuous Delivery for Python Developers
Avoid the Vendor Lock-in Trap (with App Deployment)
A guide to modern software development 2018
PyCon 9: Continuous Delivery starts at your Development Dnvironment
Painless Continuous Delivery – DjangoCon 2017
Continuous Delivery for Python Developers – PyCon Otto
Fix-Price Projects And Agile – PyCon Sette
Creating a Collaboration Platform (Leveraging the Django Eco System)
Linux für Einsteiger und UmsteigerInnen (Vortrag)
Linux auf meinem PC
Ad

Recently uploaded (20)

PDF
Salesforce Agentforce AI Implementation.pdf
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
PPTX
Cybersecurity: Protecting the Digital World
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PDF
Autodesk AutoCAD Crack Free Download 2025
PPTX
Monitoring Stack: Grafana, Loki & Promtail
PPTX
Introduction to Windows Operating System
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PDF
Time Tracking Features That Teams and Organizations Actually Need
PPTX
Custom Software Development Services.pptx.pptx
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
iTop VPN Crack Latest Version Full Key 2025
PPTX
GSA Content Generator Crack (2025 Latest)
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
"Secure File Sharing Solutions on AWS".pptx
PDF
DNT Brochure 2025 – ISV Solutions @ D365
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
Salesforce Agentforce AI Implementation.pdf
Oracle Fusion HCM Cloud Demo for Beginners
Cybersecurity: Protecting the Digital World
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Autodesk AutoCAD Crack Free Download 2025
Monitoring Stack: Grafana, Loki & Promtail
Introduction to Windows Operating System
Wondershare Recoverit Full Crack New Version (Latest 2025)
Time Tracking Features That Teams and Organizations Actually Need
Custom Software Development Services.pptx.pptx
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
iTop VPN Crack Latest Version Full Key 2025
GSA Content Generator Crack (2025 Latest)
Weekly report ppt - harsh dattuprasad patel.pptx
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
"Secure File Sharing Solutions on AWS".pptx
DNT Brochure 2025 – ISV Solutions @ D365
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM

PyCon 23: Smettiamola di scrivere script

  • 3. Dizionario Italiano a cura di Enrico Olivetti
  • 11. CLI Applications What's wrong with scripts? Why CLI Applications? 1. Argparse 2. Click 3. Docopt
  • 12. print("This is important code") 1 2 for index, arg in enumerate(sys.argv): 3 print(f"[{index}]: {arg}") 4 What's Wrong with Scripts? Easy to get started Limited possibilities for structure Hard to (unit) test No dependency management Deployment may require care Custom user experience
  • 13. print("Usage: foo <bar> --baz") def main(): 1 2 3 if __name__ == "__main__": 4 main() 5 Why CLI Applications? Standardized user experience More possibilities for structure Possibilities for all kinds of testing Dependency management Packaging & distribution
  • 14. import argparse from . import __version__ def parse_arguments(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--version', action='version', version=__version__) parser.add_argument('filename') args = parser.parse_args() return args def main(): args = parse_arguments() ... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Argparse
  • 15. import click @click.command() @click.version_option() @click.argument('filename') def main(filename): click.echo(filename) 1 2 3 4 5 6 7 Click import click @click.command() @click.version_option() @click.argument('filename', type=click.Path(exists=True)) def main(filename): click.echo(filename) 1 2 3 4 5 6 7 import click @click.command() @click.version_option() @click.argument('infile', type=click.File()) def main(infile): click.echo(infile.read()) 1 2 3 4 5 6 7
  • 16. """Foobar Usage: foobar (-h | --help | --version) foobar [-s | --silent] <file> foobar [-v | --verbose] <file> Positional arguments: file target file path name Optional arguments: -h, --help show this help message and exit -s, --silent don't show progress output -v, --verbose explain progress verbosely --version show program's version number and exit """ from docopt import docopt from . import __version__ def parse_arguments(): args = docopt(__doc__, version=__version__) return dict( file=args['<file>'], silent=args['-s'] or args['--silent'], verbose=args['-v'] or args['--verbose'], ) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Docopt
  • 17. $ copier gh:painless-software/python-cli-test-helpers pycon-italia-cli 🎤 engine (Use arrow keys) » Argparse Click Docopt ... Demo 🐍 ✨ ✨ https://python-cli-test-helpers.readthedocs.io
  • 18. Thank you! for your precious time Painless Software Less pain, more fun. Most images taken from Wikipedia (CC-SA) Alberto Sordi animated GIF stolen from giffetteria.it