• Complain

Noah Gift - Python Command Line Tools

Here you can read online Noah Gift - Python Command Line Tools full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2020, publisher: Pragmatic AI Solutions, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

No cover
  • Book:
    Python Command Line Tools
  • Author:
  • Publisher:
    Pragmatic AI Solutions
  • Genre:
  • Year:
    2020
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Python Command Line Tools: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Python Command Line Tools" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

Noah Gift: author's other books


Who wrote Python Command Line Tools? Find out the surname, the name of the author of the book and a list of all author's works by series.

Python Command Line Tools — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Python Command Line Tools" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Python Command Line Tools Design powerful apps with Click Noah Gift and Alfredo - photo 1
Python Command Line Tools
Design powerful apps with Click
Noah Gift and Alfredo Deza

This book is for sale at http://leanpub.com/pythoncli

This version was published on 2020-05-29

This is a Leanpub book Leanpub empowers authors and publishers with - photo 2

* * * * *

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do.

* * * * *

2020 Noah Gift and Alfredo Deza
Introduction

Why build complicated a complicated UI, when you can accomplish more with less code? This is the spirit of developing tools for the command-line. In this book you learn to focus on the basics of command-line tools. Ultimately the cli is the best UI. Lets get started.

Chapter 1: Getting Started with Click

Alfredo Deza

Building command-line tools is fun. For me, though, and for the longest time, it meant struggling with the different types of libraries Python had to offer. It all began with optparse, which is deprecated since Python 2.7, and it is unfortunately still in the standard library after all these years. It was replaced with argparse which, as you will see soon, it is still complicated to deal with. It is crucial to understand what Python has to offer to grasp why the alternative (the Click framework) is better.

Click is a project created by Armin Ronacher. If you havent heard of him before, you probably have heard about projects he created or co-authored:

- Pygments: Syntax highlighter
- Jinja: Templating engine
- Sphinx: Documentation framework
- Werkzeug: Server and web utility
- Babel: Internationalization library
- MarkupSafe: Safely cleans up HTML strings
- Flask: Web framework
- itsdangerous: Cryptographic library

Ive used most of these before, and have had nothing but great experiences with them. What brings us to this chapter is Click and how to get started creating powerful tooling with it.

What is wrong with the alternatives

The standard library offerings (via optparse and argparse) work fine. From this point forward, Ill briefly touch on argparse only since optparse is deprecated (I strongly suggest you avoid it). My problem with argparse is that it is tough to reason about, and it wasnt created to support some of the use cases the Click supports. One of these examples is sub-commands. Lets build a command-line

importargparsedefmain():parser=argparse.ArgumentParser(description="Tool with sub-commands")subparsers=parser.add_subparsers(help='Sub-commands')sub_parser_1=subparsers.add_parser('sub1')sub_parser_2=subparsers.add_parser('sub2')parser.parse_args()if__name__=='__main__':main()

Crafting the tool requires dealing with a class called ArgumentParser and then adding to it. I dont have anything against classes, but this reads convoluted, to say the least. Save it as sub-commands.py and run it to see how it behaves:

$ python sub-commands.py -husage: sub-commands.py [-h] {sub1,sub2} ...Tool with sub-commandspositional arguments: {sub1,sub2} Sub-commandsoptional arguments: -h, --help show this help message and exit

What are positional arguments ? I explicitly followed the documentation to add sub-commands (called sub_parsers in the code), why are these referred to as positional arguments? They arent! These are sub-commands. If you use them as a sub-command in the terminal you can verify this:

$ python sub-commands.py sub1 -husage: sub-commands.py sub1 [-h]optional arguments: -h, --help show this help message and exit

This is not good. Aside from how complex it is to deal with an instantiated class that grows with sub-commands, the implementation doesnt match what it advertises. In essence, the sub-command is calling add_subparsers(), and they display in the help output as positional arguments which are sub-commands.

It is entirely possible to change the help output to force it to say something else, but this misses the point. I found the sub-command handling of argparse so rough that I ended up creating a small library that attempts to make it a bit easier. Even though that little library is in a few production tools, I would recommend you take a close look to Click, because it does everything that argparse does, and makes it simpler - including sub-commands.

Even if you arent going to use sub-commands at all, the interface to the class and objects that configure the command-line application creates much friction. It has taken me a while to find a suitable alternative, and although Click has been around for quite some time, it hasnt been until recently that Ive acknowledged how good it is.

A helpful Hello World

The simplest use case for Click has a caveat: it doesnt handle the help menu in a way that I prefer when building command-line tools. Have you ever tried a tool that doesnt allow -h? How about it does allow -h but doesnt like --help? What about one that doesnt like -h, --help, or help?

$ find -hfind: illegal option -- husage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression] find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]$ find --helpfind: illegal option -- -usage: find [-H | -L | -P] [-EXdsx] [-f path] path ... [expression] find [-H | -L | -P] [-EXdsx] -f path [path ...] [expression]$ find helpfind: help: No such file or directory

Perhaps Im raising the bar too high and find is too old of a tool? Lets try with some modern ones:

$ docker -hFlag shorthand -h has been deprecated, please use --help$ python3 help/usr/local/bin/python3: can't open file 'help': [Errno 2] No such file or directory

Do you think this level of helpfulness is over the top? Some tools do work with all three of them, like Skopeo and Coverage.py. Not only is it possible to be that helpful, but it should also be a priority if you are creating a new tool. As I mentioned, Click doesnt do all three by default, but it doesnt take much effort to get them all working. There are lots of basic examples for Click, and in this section, Ill concentrate on creating one that allows all these help menus to show.

Starting with the most basic example so that we can check what is missing and then go fix it up, save the example as cli.py:

importclick@click.command()
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Python Command Line Tools»

Look at similar books to Python Command Line Tools. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Python Command Line Tools»

Discussion, reviews of the book Python Command Line Tools and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.