Skip to main content

A library for quickly building CLI Python-based tools with basic features in a shortened way

Project description

PyPi Read The Docs Python Versions License


This library is currently a very minimalistic module aimed to shorten and format the way a "self-contained" Python tool can be made. It is mostly based on a script template that was used for building some specific tools holding useful metadata. It is not aimed to provide helpers as many other libraries already do this.

NB: By "self-contained", it is meant that the script does not rely on relative libraries or any configuration or other file, only on Python installed libraries. Such a tool is thus contained in a single file. This is because it is more convenient for deploying its into an executable path.


sudo pip install tinyscript


Every customization MUST be declared <u>before</u> the initialize(globals()) call. Once invoked, this function appends useful references to the script's dictionary of global variables.

Customizing metadata

Metadata fields used in the documentation:

Field Comment
__author__ self-explanatory
__email__ self-explanatory
__examples__ a list of strings providing example arguments and options (no need to mention the tool name)
__reference__ field for referencing a book/course/...
__source__ same as for __reference__
__training__ field for mentioning a training the script comes from
__version__ self-explanatory

Customizing logging

Constants that can be overwritten:

Name Default
LOG_FORMAT %(asctime)s [%(levelname)s] %(message)s

Defining arguments

Import from TinyScript prevents from redefining a parser and the initialize(globals()) call achieves arguments parsing so that it only remains to add new arguments in the main script.

NB: A verbose switch is built-in by default like follows and can be overwritten:

parser.add_argument("-v", dest="verbose", action="store_true",
                    help="debug verbose level (default: false)")

This switch can also be configured for counting v's to set the debug level. In this case, append multi_debug_level=True to the initialize call.

Pre-imported modules

List of pre-imported built-in modules:

  • logging
  • os
  • random
  • re
  • signal
  • sys
  • time

Customizing the exit/interrupt/terminate handler

Handlers are defined as follows:

  • at_exit: This is executed when the script ends, whatever the reason. This can be used e.g. for closing a socket.
  • at_graceful_exit: This only runs if the script completed successfully. at_exit is executed afterwards.
  • at_interrupt: This only runs when SIGINT signal is received, in other words, when Ctrl+C is hit. at_exit is executed afterwards.
  • at_terminate: This only runs when SIGTERM signal is received, in other words, when another process kills the script. at_exit is executed afterwards.


Very simple example with no documentation and no handlers:

""" """
#!/usr/bin/env python
from tinyscript import *

__author__ = "John Doe"
__version__ = "1.0"
__reference__ = "Tinyscript documentation"
__examples__ = ["-h"]

if __name__ == '__main__':
    initialize(globals())"Hello world !")

In a terminal, it gives:

$ python 
12:34:56 [INFO] Hello world !

$ python -h
usage: [-h] [-v]

HelloWorld v1.0
Author: John Doe
Reference: Tinyscript documentation

optional arguments:
  -h, --help  show this help message and exit
  -v          debug verbose level (default: false)

Usage examples:
  python -h

Example with documentation and handlers:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = "John Doe"
__email__ = ""
__version__ = "1.0"
__reference__ = "A great book !"
__examples__ = ["-v", "-i 0"]
__doc__ = "This is an example tool"

from tinyscript import *

def at_exit():"Shutting down...")

def at_graceful_exit():"Execution successful")

def at_interrupt():

def at_terminate():

if __name__ == '__main__':
    global logger
    parser.add_argument("-i", dest="integer", type=int, default=-1,
                        help="an example integer (default: -1)")
    parser.add_argument("-k", dest="integer2", type=int, default=-1,
                        help="another example integer (default: -1)")
    initialize(globals(),  # this appends 'args' and 'logger' to globals
               sudo=False, # this allows to require for sudo or not
               # if multi_debug_level=True, this allows to use -v, -vv, -vvv
                           #  no -v  => ERROR
                           #  -v     => WARNING
                           #  -vv    => INFO
                           #  -vvv   => DEBUG
    # two kinds of validation: without default => triggers exit ;
    #                          with default    => sets the default and continues
        ("integer", " ? < 0", "Integer must be greater or equal to 0"),
        ("integer2", " ? < 0", "Same as for the other integer", 1000),
    )  # this will exit because of 'integer' whose default is -1
       # and will only give a warning for 'integer2' whose default is -1"Hit Ctrl+C to leave...")
    while True:
        pass  # Ctrl+C will use at_interrupt()
              # at_exit() will then execute

# 'python' will fail
# 'python -i 0 -v' will show a warning for 'integer2' (because of -v)
# 'python -i 0 -k 0' will work with no warning

Project details

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for tinyscript, version 1.3.0
Filename, size File type Python version Upload date Hashes
Filename, size tinyscript-1.3.0.tar.gz (31.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page