A talk to be given at the Python WA meetup on 2021-11-04
Do you prefer to spend your time writing a program, or writing the interface layer (which handles the command-line options and subcommands, tests them for validity, opens files, etc.)?
Wouldn't it be great if you could just indicate how your command-line interface should work and let something else take care of the details? Good news: Click can do this! It uses decorators, so as to keep your interface functions concerned only with business logic, i.e. dealing with the problem domain. Click has a large set of classes that make it easy to specify the type of your command-line options and arguments, and create custom types if you want. And it generates online help messages, which can be enhanced by optional descriptions that you add to your option specifications.
This talk will give an overview of how to create a Python command-line tool using setup.py packaging, with entry points.
https://click.palletsprojects.com
- Introduction
- Click rationale
- The basics
- Options
- Boolean options
- Options with parameters
- Integer options
- Counting options
- Mandatory options
- Choice options
- File path options
- File handle options
- Multi-string options
- Password option
- Prompting when an option is missing
- Positional parameters
- Lists of parameters
- Variable numbers of parameters
- Click types
- Subcommands
- Contexts
- Defaults and the environment
- Colours, logging, prompting, exceptions, oh my!
- Conclusion
- Questions
Alastair is a Software Engineer and system administrator by trade. He has a BSc in Computer Science from Curtin University.
His computer-related interests lie in various areas within his trade; suffice to say that he is a "geek of many colours". :) Alastair is a die-hard FOSS user and Linux fan.
He is also a freelancer with his own business. Warpspace IT is a consultancy with a fairly broad focus on the technical side of IT.
1300 881744
[Python-Click-talk slides.odp](Python-Click-talk slides.odp)