Skip to main content

Make a command-line interface from a function.

Project description

Produce a command-line interface from a Python function.

Consider the following Python program, called do_something.py.

#!/usr/bin/env python
def main(input_file, output_file, n_cores:int = 3):
    '''
    Do something to a file with several cores.
    '''
    # Pretend that something happens here.
horetu(main)

Run it with the help flag; this is the result.

$ do_something.py --help
usage: do_something.py [-h] [--n-cores N-CORES]
                       input-file output-file

Do something to a file with several cores.

Here’s a more complicated example.

#!/usr/bin/env python

# ...

def main():
    commands = {
        'scrape': scrape,
        'serve': server.main,
        'set-password': set_password,
        'save-hucs': scraper.save_hucs,
    }
    description = 'Catalog and process public notices for Section 404 permit applications.'
    horetu(commands, name = 'scott', description = description)

Then we can run it like this.

$ scott -h
usage: scott [-h] {serve,set-password,scrape,save-hucs} ...

Catalog and process public notices for Section 404 permit applications.

positional arguments:
  {serve,set-password,scrape,save-hucs}

optional arguments:
  -h, --help            show this help message and exit

You can have nested subcommands too.

f = g = h = i = j = lambda x: int(x) + 4
commands = {
    'subcommand1': {
        'subsubcommand1.1': f,
        'subsubcommand1.2': g,
    },
    'subcommand2': h,
    'subcommand3': {
        'subsubcommand3.1': i,
        'subsubcommand3.2': {
            'subsubsubcommand3.2.1': j,
        }
    },
}
horetu(commands)

Project details


Download files

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

Source Distribution

horetu-0.0.2.tar.gz (1.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page