Oh behave!

Build reliable command line tools

with `click`

Stefan Antoni @suchkultur

Be a good boy script!

$> find ~/.virtualenvs -type f | grep ".*/site-packages/django/__init__.py$" | wc -l
8

$> echo $?

0

Requirements

for great command line tools:

 

  • Read input from STDIN
  • Write output to STDOUT
  • Errors go to STDERR
    • and set the exit() value from to e.g. 1 or else
  • No headers, no superflous decoration foo
  • Output == API
    • Don't break the API by changing the format

Awesome tools!

... will be used in ways you never imagined before!

# listen on port 25, wait for an incoming connection, use CR+NL on this
# connection, relay data to and from stdio, then emulate 
# a mailserver by hand :-)
socat - TCP-LISTEN:25,crlf
# Copy all *.txt files which contain "bob" to a new directory
find . -type f -name "*.txt" | grep 'bob' | xargs cp –target-directory=newDir

​
# Extract palindromes from a website and print them (i show you later)
curl "http://de.wikipedia.org/wiki/Liste_deutscher_Palindrome" -o - | ./palindrome.py - -

Don't reinvent the wheel (again)

I tried Armin Ronachers

library "click" (thanks @ulope)

  • Click parses and dispatches

    • ​tighter coupling,  more context

  • Easy things are easy
  • Complex things are painless
  • Click is "complete"

  • click is @fun()

./palindrome.py --help (click)

python-cli-with-click

By Stefan Antoni

python-cli-with-click

A quick talk about good command line tools and how the click module can help to build them in python.

  • 701
Loading comments...

More from Stefan Antoni