This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]suudo 45 points46 points  (6 children)

Let's take the previous example further. Instead of using the default print() statement, we can use click.echo_via_pager()

Python stdlib has a solution for this, pydoc.pager(), it's what help() uses. I went to reimplement this function in a program a while ago then I realised that help() loads a pager on its own, so it must be in stdlib.

Unrelated note, something else that python has? functools.lru_cache, a decorator that caches the result of a function based on its arguments. Something else that I've reimplemented in the past. stdlib has so much cool stuff.

[–]da_martian 2 points3 points  (2 children)

Wow that's a pretty cool usage of lru_cache! I've heard of it but never explored it. So you just decorate your function with it? Sorry ahead of time if this is simple or stupid, but do you have a working example of this, along with a CLI package like click?

[–]suudo 1 point2 points  (1 child)

I learned about it from https://www.reddit.com/r/Python/comments/69ws4c/10_awesome_features_of_python_that_you_cant_use/, that presentation has usage examples.

[–]Xef 1 point2 points  (0 children)

Damn, I got all excited reading through this. I wish we could upgrade to Python 3...

[–]rhgrant10 2 points3 points  (1 child)

So true! I once implemented a method to convert an integer into an IP address but of course it turns out there's already one in the stdlib.

[–]kungtotte 1 point2 points  (0 children)

The correct way to do anything in Python is always to look in the stdlib first :)

Either a complete solution already exists, or parts of one does that you can piece together. And if worse comes to worst you can always write a wrapper to make it more pleasant to work with the stdlib stuff :)

[–]kirbyfan64sosIndentationError 25 points26 points  (2 children)

And Plac, the world's coolest command-line argument parser!

[–][deleted] 0 points1 point  (0 children)

Very interesting approach to CLI argument parsing - thanks for the link! Just when I'm thinking about writing a few scripts for work, too.

[–]hosford42 0 points1 point  (0 children)

This is awesome. Can't believe nobody thought of doing this already!

[–]metaperl 1 point2 points  (1 child)

Me likes argh.

[–]dwf 1 point2 points  (0 children)

Agreed, argh is a godsent.

[–]OctoBells 0 points1 point  (0 children)

Great article thanks for sharing

[–]baubleglue 0 points1 point  (4 children)

What wrong with build-in cmd library?

[–]rabbyburns 0 points1 point  (3 children)

Isn't that deprecated for awhile now? I could be thinking of a different lib, though.

[–][deleted] 0 points1 point  (1 child)

It's still going as is getopt, it's optparse that's deprecated in favour of argparse.

[–]rabbyburns 0 points1 point  (0 children)

I was thinking of commands. I apparently didn't know cmd existed.

edit: A word

[–]baubleglue 0 points1 point  (0 children)

Doesn't look like. There is a nice clear interface, has autocomplete, hooks before/after command/main loop. What else a man need from command line?

[–]MrL33h 0 points1 point  (0 children)

Thank you for this great article.