• Stars
    star
    134
  • Rank 262,313 (Top 6 %)
  • Language
    Python
  • License
    BSD 2-Clause "Sim...
  • Created almost 15 years ago
  • Updated almost 15 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Syntactic sugar for creating Python command line scripts by introspecting a function definition
optfunc
=======

Parse command line options in Python using function introspection.

Post feedback here: http://simonwillison.net/2009/May/28/optfunc/

I can never remember how to use any of Python's regular command line parsing
libraries.

optfunc uses introspection to make a Python function available as a command
line utility. It's syntactic sugar around optparse from the standard library.

Here's what the API looks like so far:

    import optfunc
    
    def upper(filename, verbose = False):
        "Usage: %prog <file> [--verbose] - output file content in uppercase"
        s = open(filename).read()
        if verbose:
            print "Processing %s bytes..." % len(s)
        print s.upper()
    
    if __name__ == '__main__':
        optfunc.run(upper)

And here's the resulting command-line interface:

    $ ./demo.py --help
    Usage: demo.py <file> [--verbose] - output file content in uppercase
    
    Options:
      -h, --help     show this help message and exit
      -v, --verbose  
    $ ./demo.py README.txt 
    OPTFUNC
    ...
    $ ./demo.py README.txt -v
    Processing 2049 bytes...
    OPTFUNC
    ...

If you don't mind relying on some stack inspecting magic, you can replace the 
__name__ == '__main__ idiom with the following:

    optfunc.main(upper)

If you like really short scripts, you can even use this function as a 
decorator:

    @optfunc.main
    def upper(filename):
        print open(filename).read().upper()

How arguments work
------------------

Non-keyword arguments are treated as required arguments - optfunc.run will 
throw an error if they number of arguments provided on the command line 
doesn't match the number expected by the function (unless @notstrict is used, 
see below).

Keyword arguments with defaults are treated as options. At the moment, only 
string and boolean arguments are supported. Other types are planned.

Consider the following:

    def geocode(s, api_key='', geocoder='google', list_geocoders=False):

's' is a required argument. api_key, geocoder and list_geocoders are all 
options, with defaults provided. Since list_geocoders has a boolean as its 
default it will be treated slightly differently (in optparse terms, it will 
store True if the flag is provided on the command line and False otherwise).

The command line options are derived from the parameter names like so:

    Options:
      -h, --help            show this help message and exit
      -l, --list-geocoders
      -a API_KEY, --api-key=API_KEY
      -g GEOCODER, --geocoder=GEOCODER

Note that the boolean --list-geocoders is a flag, not an option that sets a
value.

The short option is derived from the first letter of the parameter. If that 
character is already in use, the second character will be used and so on.

The long option is the full name of the parameter with underscores converted 
to hyphens.

If you want complete control over the name of the options, simply name your 
parameter as follows:

    def foo(q_custom_name=False):

This will result in a short option of -q and a long option of --custom-name.

Special arguments
-----------------

Arguments with the names 'stdin', 'stdout' or 'stderr' will be automatically 
passed the relevant Python objects, for example:
    
    #!/usr/bin/env python
    # upper.py
    import optfunc
    
    @optfunc.main
    def upper_stdin(stdin, stdout):
        stdout.write(stdin.read().upper())

Does the following:

    $ echo "Hello, world" | ./upper.py
    HELLO, WORLD

Subcommands
-----------

Some command line applications feature subcommands, with the first argument 
to the application indicating which subcommand should be executed.

optfunc has the beginnings of support for this - you can pass an array of 
functions to the optfunc.run() and the names of the functions will be used 
to select a subcommand based on the first argument:

    import optfunc
    
    def one(arg):
        print "One: %s" % arg
    
    def two(arg):
        print "Two: %s" % arg
    
    def three(arg):
        print "Three: %s" % arg
    
    if __name__ == '__main__':
        optfunc.run([one, two, three])

Usage looks like this:

    $ ./subcommands_demo.py    
    Unknown command: try 'one', 'two' or 'three'
    $ ./subcommands_demo.py one
    one: Required 1 arguments, got 0
    $ ./subcommands_demo.py two arg
    Two: arg

This approach is limited in that help can be provided for an individual option 
but not for the application as a whole. If anyone knows how to get optparse to
handle the subcommand pattern please let me know.

Decorators
----------

optfunc also supports two decorators for stuff I couldn't work out how to 
shoehorn in to a regular function definition. geocode.py shows them in action:

    @optfunc.notstrict
    @optfunc.arghelp('list_geocoders', 'list available geocoders and exit')
    def geocode(s, api_key='', geocoder='google', list_geocoders=False):
        # ...

@notstrict means "don't throw an error if one of the required positional 
arguments is missing" - in the above example we use this because we still want
the list_geocoders argument to work even if a string has not been provided.

@arghelp('arg-name', 'help text') allows you to provide help on individual 
arguments, which will then be displayed when --help is called.

TODO
----

* Support for different argument types (int, string, filehandle, choices)
* Special handling for 'stdin' as an argument name
* Proper unix error semantics (sys.exit(1) etc)
* Allow the function to be a generator, print iterations to stdout
* Support for *args (I don't think **kwargs makes sense for optfunc)
* Subcommands need to interact with --help better

More Repositories

1

datasette

An open source multi-tool for exploring and publishing data
Python
7,807
star
2

sqlite-utils

Python CLI utility and library for manipulating SQLite databases
Python
1,191
star
3

shot-scraper

A command-line utility for taking automated screenshots of websites
Python
1,006
star
4

csvs-to-sqlite

Convert CSV files into a SQLite database
Python
758
star
5

til

Today I Learned
HTML
719
star
6

django-sql-dashboard

Django app for building dashboards using raw SQL queries
Python
400
star
7

simonw

https://simonwillison.net/2020/Jul/10/self-updating-profile-readme/
Python
362
star
8

llm

Access large language models from the command-line
Python
309
star
9

db-to-sqlite

CLI tool for exporting tables or queries from any SQL database to a SQLite file
Python
302
star
10

djangode

Utilities functions for node.js that borrow some useful concepts from Django
JavaScript
256
star
11

csv-diff

Python CLI tool and library for diffing CSV and JSON files
Python
238
star
12

datasette-lite

Datasette running in your browser using WebAssembly and Pyodide
HTML
237
star
13

shot-scraper-template

Template repository for setting up shot-scraper
217
star
14

geocoders

Ultra simple API for geocoding a single string against various web services.
Python
184
star
15

ca-fires-history

Tracking fire data from www.fire.ca.gov
165
star
16

django-openid

A modern library for integrating OpenID with Django - incomplete, but really nearly there (promise)
Python
163
star
17

openai-to-sqlite

Save OpenAI API results to a SQLite database
Python
161
star
18

action-transcription

A tool for creating a repository of transcribed videos
Python
158
star
19

s3-credentials

A tool for creating credentials for accessing S3 buckets
Python
149
star
20

git-history

Tools for analyzing Git history using SQLite
Python
147
star
21

google-drive-to-sqlite

Create a SQLite database containing metadata from Google Drive
Python
142
star
22

django-queryset-transform

Experimental .transform(fn) method for Django QuerySets, for clever lazily evaluated optimisations.
Python
142
star
23

ratelimitcache

A memcached backed rate limiting decorator for Django.
Python
141
star
24

djng

Turtles all the way down
Python
129
star
25

cougar-or-not

An API for identifying cougars v.s. bobcats v.s. other USA cat species
Jupyter Notebook
119
star
26

simonwillisonblog

The source code behind my blog
JavaScript
118
star
27

advent-of-code-2022-in-rust

Copilot-assisted Advent of Code 2022 to learn Rust
Rust
114
star
28

djangopeople.net

A geographical community site for Django developers.
Python
111
star
29

scrape-chatgpt-plugin-prompts

Shell
107
star
30

s3-ocr

Tools for running OCR against files stored in S3
Python
103
star
31

datasette-app

The Datasette macOS application
JavaScript
100
star
32

django-redis-monitor

Request per second / SQLop per second monitoring for Django, using Redis for storage
Python
97
star
33

python-lib

Opinionated cookiecutter template for creating a new Python library
Python
97
star
34

ttok

Count and truncate text based on tokens
Python
96
star
35

mytweets

Script for saving a JSON archive of your tweets.
Python
81
star
36

airtable-export

Export Airtable data to YAML, JSON or SQLite files on disk
Python
79
star
37

datasette-graphql

Datasette plugin providing an automatic GraphQL API for your SQLite databases
Python
77
star
38

llm-mlc

LLM plugin for running models using MLC
Python
74
star
39

strip-tags

CLI tool for stripping tags from HTML
Python
73
star
40

django_cropper

Integration of jCrop with the Django admin
Python
71
star
41

click-app

Cookiecutter template for creating new Click command-line tools
Python
70
star
42

datasette-ripgrep

Web interface for searching your code using ripgrep, built as a Datasette plugin
Python
69
star
43

download-esm

Download ESM modules from npm and jsdelivr
Python
67
star
44

datasette.io

The official project website for Datasette
HTML
66
star
45

ftfy-web

Paste in some broken unicode text and FTFY will tell you how to fix it!
Python
63
star
46

markdown-to-sqlite

CLI tool for loading markdown files into a SQLite database
Python
63
star
47

sqlite-diffable

Tools for dumping/loading a SQLite database to diffable directory structure
Python
62
star
48

sqlite-history

Track changes to SQLite tables using triggers
Python
62
star
49

yaml-to-sqlite

Utility for converting YAML files to SQLite
Python
62
star
50

covid-19-datasette

Deploys a Datasette instance of COVID-19 data from Johns Hopkins CSSE and the New York Times
Python
61
star
51

dogproxy

Experimental HTTP proxy (using node.js) for avoiding the dog pile effect.
JavaScript
61
star
52

soupselect

CSS selector support for BeautifulSoup.
Python
60
star
53

laion-aesthetic-datasette

Use Datasette to explore LAION improved_aesthetics_6plus training data used by Stable DIffusion
Python
57
star
54

datasette-cluster-map

Datasette plugin that shows a map for any data with latitude/longitude columns
JavaScript
55
star
55

action-transcription-demo

A tool for creating a repository of transcribed videos
Python
53
star
56

datasette-vega

Datasette plugin for visualizing data using Vega
JavaScript
52
star
57

pge-outages-pre-2024

Tracking PG&E outages
Python
51
star
58

google-calendar-to-sqlite

Create a SQLite database containing your data from Google Calendar
Python
50
star
59

url-map

Use URL parameters to generate a map with markers, using Leaflet and OpenStreetMap
HTML
49
star
60

disaster-scrapers

Scrapers for disaster data - writes to https://github.com/simonw/disaster-data
Python
46
star
61

geojson-to-sqlite

CLI tool for converting GeoJSON files to SQLite (with SpatiaLite)
Python
45
star
62

asgi-csrf

ASGI middleware for protecting against CSRF attacks
Python
44
star
63

datasette-chatgpt-plugin

A Datasette plugin that turns a Datasette instance into a ChatGPT plugin
Python
44
star
64

nodecast

A simple comet broadcast server, originally implemented as a demo for Full Frontal 2009.
JavaScript
44
star
65

bugle_project

Group collaboration tools for hackers in forts.
Python
42
star
66

django-html

A way of rendering django.forms widgets that differentiates between HTML and XHTML.
Python
42
star
67

datasette-auth-github

Datasette plugin that authenticates users against GitHub
Python
41
star
68

puppeteer-screenshot

Vercel app for taking screenshots of web pages using Puppeteer
JavaScript
40
star
69

llm-replicate

LLM plugin for models hosted on Replicate
Python
40
star
70

python-lib-template-repository

GitHub template repository for creating new Python libraries, using the simonw/python-lib cookiecutter template
39
star
71

django-signed

Signing utilities for Django, to try out an API which is being proposed for inclusion in Django core.
Python
37
star
72

museums

A website recommending niche museums to visit
JavaScript
36
star
73

pypi-rename

Cookiecutter template for creating renamed PyPI packages
Python
36
star
74

help-scraper

Record a history of --help for various commands
Python
35
star
75

dbf-to-sqlite

CLI tool for converting DBF files (dBase, FoxPro etc) to SQLite
Python
35
star
76

asyncinject

Run async workflows using pytest-fixtures-style dependency injection
Python
35
star
77

disaster-data

Data scraped by https://github.com/simonw/disaster-scrapers
35
star
78

datasette-publish-vercel

Datasette plugin for publishing data using Vercel
Python
34
star
79

gzthermal-web

A web interface to gzthermal by caveman on encode.ru
Python
32
star
80

asgi-auth-github

ASGI middleware that authenticates users against GitHub
Python
31
star
81

json-head

JSON microservice for performing HEAD requests
Python
31
star
82

s3-image-proxy

A tiny proxy for serving and resizing images fetched from a private S3 bucket
Python
31
star
83

django-safeform

CSRF protection for Django forms.
Python
31
star
84

sqlite-transform

Tool for running transformations on columns in a SQLite database
Python
30
star
85

webhook-relay

A simple Node.js server for queueing and relaying webhook requests
JavaScript
30
star
86

datasette-tiddlywiki

Run TiddlyWiki in Datasette and save Tiddlers to a SQLite database
HTML
29
star
87

image-diff

CLI tool for comparing images
Python
29
star
88

sf-tree-history

Tracking the history of trees in San Francisco
29
star
89

getlatlon.com

Source code for getlatlon.com - a simple, single page, pure JavaScript Google Maps application.
29
star
90

scrape-hacker-news-by-domain

Scrape HN to track links from specific domains
JavaScript
28
star
91

timezones-api

A Datasette-powered API for finding the time zone for a latitude/longitude point
Python
26
star
92

owlsnearme

A website that tells you where your nearest owls are!
JavaScript
26
star
93

datasette-table

A Web Component for embedding a Datasette table on a page
JavaScript
26
star
94

xml-analyser

Simple command line tool for quickly analysing the structure of an arbitrary XML file
Python
26
star
95

shapefile-to-sqlite

Load shapefiles into a SQLite (optionally SpatiaLite) database
Python
26
star
96

cdc-vaccination-history

A git scraper recording the CDC's Covid Data Tracker numbers on number of vaccinations per state.
Python
24
star
97

json-flatten

Python functions for flattening a JSON object to a single dictionary of pairs, and unflattening that dictionary back to a JSON object
Python
24
star
98

datasette-json-html

Datasette plugin for rendering HTML based on JSON values
Python
24
star
99

shot-scraper-demo

Live demo of shot-scraper
23
star
100

djangocon-2022-productivity

Supporting links for my DjangoCon 2022 talk
23
star