The Unofficial Guide to ...
Building Command-Line Utilities in Python¶
“Arguably, Python is the best language for making UNIX® command line tools, period, due to its batteries-included philosophy, and its emphasis on readable code. Just a warning, though; these are dangerous ideas when you find out how easy it is to create a command line tool in Python, you might be spoiled for life.” [Gift08]
@command()
def hello():
print('You might be spoiled for life!')
Hint
A function becomes a command line tool by decorating it with @command. At its simplest, just decorating a function with this decorator will make it into a callable script. [Rona17]
Choose Your Preferred Learning Style¶
- Just show me the code!
- I want to design better command-line utilities.
- I want to watch Click eat its own dog food.
- I’m coming from the world of argparse.
- I’m coming from the world of docopts.
- I just want a cheatsheet & reference material.
Table of Contents¶
- Guidelines: POSIX Guidelines, GNU Standards & Portability
- Structures: Command-Line Utility Design Structures
- Patterns: Command-Line Interface Design Patterns
- Wordsplitting: How Your Shell Splits Command-Line Arguments
- Parsing: Arguments/Options and the 5 Types of Dashes
- Arguments: From N to M, Lists & Consumption
- Options: All About Options and the Dirty Dozen
- Naming: Best Practices
- Help: Anatomy of a Help Screen
- Types: Validating User Input Early (Conversion, Validation & Transformation)
- I/O: Bytes in the Wild – To Unicode and Back
- SoC: Separation of Concerns
- Summary: Putting it All Together
- References