Skip to main content

Making a CLI Application by wrapping

Project description

wrapp: Making a CLI Application by wrapping

INSTALL

pip install wrapp

USAGE

TL;DR

  1. Create your Python script under a few rules. To do so, start with wrapp.new.

    wrapp.new > YOURS.py
    
  2. Edit YOURS.py as you like.

  3. Then you can run your Python script as an CLI app.

    wrapp YOURS.py
    

That's it. Let's enjoy !

Create your Python script under a few rules

By using wrapp.new,

wrapp.new > YOURS.py

you can get a template Python file named YOURS.py.

wrapp.new outputs template code at stdout.

$ cat YOURS.py
#!/usr/bin/env python3
from logging import getLogger


logger = getLogger(__name__)


def add_arguments(parser):
    ...


def main(args):
    ...

Starting with this template, add program options in add_arguments(parser).
The type of parser is assumed as argparse.ArgumentParser class.

And main(args) function is the entry point. When you run wrapp YOURS.py, the program arguments are parsed as defined in add_arguments(parser) and stored in the variable named args. Then all program arguments and options are output via logger. Finally, the main(args) is called.

As shown above, wrapp assumes your Python file contains add_arguments(parser) and main(args). logger is optional. Also logger can be replaced its name as _LOG or LOG. For logger, it's OK to use any other 3rd-party logging modules like loguru.

Run your Python script as an CLI app

Assume your Python script is YOURS.py.

wrapp YOURS.py --your-options ...

That is, just replace python to wrapp. Then you can keep your script simple:

  • if __name__ == '__main__': is not needed.
  • Also you don't need any noisy modules such as argparse, from argparse import ..., from logging import ....

Debugging

When you want to debug your script, run the code like this.

python3 -m pdb -m wrapp YOURS.py --your-options ...

Then the debugging mode (pdb) will be started.

FEATURES

  • No dependencies. wrapp only depends on Python standard libraries.

  • One file. If you don't need to install other packages at all, just copy src/wrapp/wrapp.py.

    $ cp PATH/TO/wrapp_repo/src/wrapp/wrapp.py ./wrapp
    $ chmod u+x wrapp
    $ ./wrapp.new > YOURS.py
    $ ./wrapp YOURS.py
    
  • It's like python-fire. But for wrapp, you don't need to import any other module in your Python code.

  • It's trivial but you also run your script without the extension; wrapp YOURS.

LICENSE

MIT License.

BACKGROUNDS

As I wrote tons of Python CLI applications, I noticed that,

  • argparse is the best practice to add my program command options.
  • logging is not bad if I modify something (format, ...).
  • But I noticed that there are many similar lines in my applications. And they make my code more dirty.

Here is my application code pattern. Please note that there is nothing infomative.

#!/usr/bin/env python3
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from logging import getLogger
from pathlib import Path
import logging.config


_LOG = getLogger(__name__)


def add_arguments(parser):
    parser.add_argument(
            'in_file', type=Path,
            help='An input file.')
    parser.add_argument(
            '--out-dir', '-d', type=Path, default=None,
            help='A directory.')


def _main(args):
    _LOG.debug('debug')
    _LOG.info('info')
    _LOG.warning('warning')
    _LOG.error('error')
    _LOG.critical('critical')
    ...


def _parse_args():
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    parser.add_argument('-i', '--input-files', nargs='*', help='input files.')
    args = parser.parse_args()
    logging.config.fileConfig('logging.conf')
    for k,v in vars(args).items():
        _LOG.info('{}= {}'.format(k, v))
    return args


def _print_args(args):
    for k, v in vars(args).items():
        _LOG.info(f'{k}= {v}')


if __name__ == '__main__':
    parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
    add_arguments(parser)
    args = parser.parse_args()
    logging.config.fileConfig('logging.conf')
    _print_args(args)
    _main(args)

So I decided to separate it to 2 files; one is the contents only and the other is a wrappter to make any Python files an CLI app.

Finally, I can make the above code much more simple,

#!/usr/bin/env python3
from logging import getLogger
from pathlib import Path


_LOG = getLogger(__name__)


def add_arguments(parser):
    parser.add_argument(
            'in_file', type=Path,
            help='An input file.')
    parser.add_argument(
            '--out-dir', '-d', type=Path, default=None,
            help='A directory.')


def main(args):
    _LOG.debug('debug')
    _LOG.info('info')
    _LOG.warning('warning')
    _LOG.error('error')
    _LOG.critical('critical')
    ...

It's similar to python-fire.

But when I used the fire, I have to insert from fire import Fire and Fire(your_func). I'd like to remove even such a few code.

Then I'm completly free from noisy modules / code !

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

wrapp-0.4.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

wrapp-0.4.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file wrapp-0.4.2.tar.gz.

File metadata

  • Download URL: wrapp-0.4.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.15.0-47-generic

File hashes

Hashes for wrapp-0.4.2.tar.gz
Algorithm Hash digest
SHA256 18298d867644de592223ed038573cee45a5abf7882ac53a5b22a0b0f98a9e199
MD5 7ba258388e311599b18ed91b9f09c86d
BLAKE2b-256 a5cf63fc7644bd80510ac67eaf57803a44764bf6ad08d972bee79b2a8572b926

See more details on using hashes here.

File details

Details for the file wrapp-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: wrapp-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.15.0-47-generic

File hashes

Hashes for wrapp-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d3fb9043b1815f1cba7e7dbd45f67cf14d32730d72f6b618a19bd1cbb2f4880c
MD5 378af60c7157ef3e8cf9173d7d585534
BLAKE2b-256 2bcebb7ff34d40390e4ee532ff4084b7d036b0ddfdc602329fcb0685a1c8c00e

See more details on using hashes here.

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