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 details)

Uploaded Source

File details

Details for the file horetu-0.0.2.tar.gz.

File metadata

  • Download URL: horetu-0.0.2.tar.gz
  • Upload date:
  • Size: 1.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for horetu-0.0.2.tar.gz
Algorithm Hash digest
SHA256 08eebc5a910d00af89c1395cfef29274c404ad04ae922effdfe359d76b537120
MD5 e9588bcbb1ad5709399fe574dd80aeac
BLAKE2b-256 7000b8f4cebcd2ca907781c7a74770943ed9f6216f5ee8cde20a4b084676ca37

See more details on using hashes here.

Provenance

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