Git Product home page Git Product logo

click-boilerplate's Introduction

Click Boilerplate

Boilerplate project for a the Python Click library. This code can serve as a starting point for your project. To use it, rename the cli_app module to the name of your project and update the setup.py to reflect these changes.

Using this boilerplate

In order to use this boilerplate, I recommend doing the following:

  • Change the name of the cli_app module to something that is relevant to your project
  • Update the setup.py file, pointing the console_scripts option to the updated module name
  • Update the setup.py file with other values relevant to your project
  • Remove the hello command and start replacing with commands for your project

Installing your CLI program

Development

While developing your CLI program you will want to install it via setup.py using the following method:

$ python setup.py develop

Afterwards, your CLI program will be installed. If you have installed the default hello command, a new CLI program called cli_app will be available. Here's how you run it:

$ cli_app hello Name
> Hello Name!

The main benefit with this method is not having to reinstall everytime a change is made.

Productions (a.k.a. "for-realsies")

Follow the regular method to install your CLI program into a virtual environment or system Python:

$ python setup.py install

Adding more commands

In order to add more commands, I recommend adding them to the cli_app.commands module as separate files. Adding them to this directory and as separate files will help keep your project neat and organized.

But how do I do it?

Great question! Let's say I wanted to add a new command called weather that would fetch the current weather forecast for a city, I would do the following:

  1. Create a new file called weather.py underneath the cli_app/commands directory
  2. Put the following code in that file:
import click

@click.command()
@click.argument('city', nargs=-1)
def weather(city):
    """
    Command that retrieves the weather for a given city
    """
    click.echo('75 and SUNNY!!!')
  1. Let's create a shortcut so it's easier to reference this module later by inserting the following in cli_app/commands/__init__.py
# This line should already exist
from .hello import hello
# New entry
from .weather import weather
  1. Now the final step. Add the following near the bottom of cli_app/__init__.py
# All the boilerplate stuff is up here...

# Add Commands
# This line was already here
cli.add_command(commands.hello)
# New entry
cli.add_command(commands.weather)

If you have installed the CLI program using the "development" method from above, you should now be able to run your new sub command:

$ cli_app weather Toronto Canada
> 75 and SUNNY!!!

Check out the slides

Here are the slides that accompany this repository:

click-boilerplate's People

Contributors

travishathaway avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.