Live data from Hacker News

Click – Python library for command-line interfaces

click.pocoo.org

11–20 of 105 posts

Re: Click – Python library for command-line interfaces

#11
post #7

Looks like a nice wrapper around argparse: https://docs.python.org/3/library/argparse.html

It's a wrapper around optparse, as per http://click.pocoo.org/why/.

I'm curious about that decision given that optparse is deprecated. It says it's because argparse doesn't allow nested commands, but I don't really see why that feature is so desirable as to warrant using a deprecated module. I probably need to have a play with it to find out.

Looks like a nice module.

Re: Click – Python library for command-line interfaces

#12
post #8

It looks really well done. I still prefer docopt ( https://github.com/docopt/docopt ) . It is so much simpler to use. Click seems to be a bit overengineered.

As someone who has never used either, the documentation and simple example of Click won hands down.

I could use it for simple cases within 15 seconds of reading. Docopt not so much.

Re: Click – Python library for command-line interfaces

#13
post #4

Does anybody know docopt? I really like it because you simply write the help/usage as text and docopt automatically generates the parser for it. Take a look at the example in the README: https://github.com/docopt/docopt

I'm using docopt....It's really nice with it's automagicness. But for some stuff it's just not enough, too unflexible. I'm really excited about click.

Re: Click – Python library for command-line interfaces

#14
post #8

It looks really well done. I still prefer docopt ( https://github.com/docopt/docopt ) . It is so much simpler to use. Click seems to be a bit overengineered.

As someone who has never used either, the documentation and simple example of Click won hands down. I could use it for simple cases within 15 seconds of reading. Docopt not so much.

You're right - looking on the http://docopt.org/ page it's not immediately obvious how it works.

The general idea is that you don't specify any code, you just give the help message as text. Docopt parses that to figure out all the options etc and convert those into the parameters coming in to your system.

It's a really clever idea. You specify the human interface, docopt converts that into the code version (so long as you adhere to a few common conventions). I haven't seen a cleaner system anywhere.

From their example:

    """Naval Fate.
    
    Usage:
      naval_fate.py ship new ...
      naval_fate.py ship  move   [--speed=]
      naval_fate.py ship shoot  
      naval_fate.py mine (set|remove)   [--moored | --drifting]
      naval_fate.py (-h | --help)
      naval_fate.py --version
    
    Options:
      -h --help     Show this screen.
      --version     Show version.
      --speed=  Speed in knots [default: 10].
      --moored      Moored (anchored) mine.
      --drifting    Drifting mine.
    
    """
    from docopt import docopt
    
    
    if __name__ == '__main__':
        arguments = docopt(__doc__, version='Naval Fate 2.0')
        print(arguments)

Re: Click – Python library for command-line interfaces

#16
post #8

It looks really well done. I still prefer docopt ( https://github.com/docopt/docopt ) . It is so much simpler to use. Click seems to be a bit overengineered.

I've used docopt for some tools, but argtools (and click it seems) is much more convenient, imo. It's very easy to remember the 1-2 decorators and probably most importantly, the command arguments and everything else are where the command is defined, not somewhere else.

When using docopt i spent much more time defining the docstring and figuring out the right way to write it, especially more complex scenarios.

Re: Click – Python library for command-line interfaces

#17
The biggest gripe I have with using Python for rich command-line tool is the startup time. One of the first reason I like to write a nice command-line tool when I start a project, say foo, is to be able to do `foo --help` to quickly see and remember what the project can do (I have a very bad memory and doing this makes it possible for me to jump back faster to a project, even well documented. I can forget what I was doing in just a few days and so I add a lot of small commands).

In short running `foo --help` should be instant and if it loads all its modules to list the different sub-commands and their respective description it is really too slow.

A possibility is to cache some information (e.g. generate a text file or a small Python script).

Re: Click – Python library for command-line interfaces

#19
post #6

Kudos again to the pocoo team for creating such a simple and useful library. It sure beats optparse :) On a side note, however, does anyone know why the team prefers to wrap functions in decorators? Flask also uses them, but what's the design decision behind them?

Decorators are so simple and minimalistic and elegant and easy to understand. They can surface an arbitrary amount of extra functionality with a single statement. For example, ensure a function is only accessible to authenticated users by simply saying something like @requires_authenticated_user. Another example would be to create a logging decorator to wrap a function or a class to create a log of every time the fun…

Yep, it's best us case is basically acting as pythons anonymous function (since lambdas suck for everything but the most trivial things).

Where in js flask might read app.route('/', function(){//do something}); python uses a decorator right above a regular function definition. The alternative is to manually add it after it's defined (in flask that's add_url_rule), but that kind of hides the intent of the function and is more cumbersome as you need to manually pass in the fn name.

Re: Click – Python library for command-line interfaces

#20
post #8

It looks really well done. I still prefer docopt ( https://github.com/docopt/docopt ) . It is so much simpler to use. Click seems to be a bit overengineered.

I love docopt. My big draw is that I write the documentation only once - in the doc string where it should be. Click looks great, but there is no documentation in the code itself. You need to run the code to print the documentation.
Post reply on HN