Skip to main content

An enhancement of argparse package for its simplest usages

Project description

simple-cli-args

A python main method decorator.
An enhancement of the argparse package for its simplest usages.
Requires python 3.6 or higher.

The ordinary arguments become positional, the arguments with default value become named argument for the CLI, with a possibility of abbreviations, as argparse provides it.
Help option (-h or --help) is automatically generated with its text taken from the docstrings.

Install

Using pip

pip install simple_cli_args

or, with your intended python command in place of python3

python3 -m pip install simple_cli_args

Using setuptools

Simply issue in the main directory of the cloned git repository:

./setup.py install

Usage

Assume the content of my_cli.py is:

#!/usr/bin/env python3
from simple_cli_args import cli_args

@cli_args
def main(apple, banana, cucumber='green'):
    print("Our fruits are:", apple, banana, cucumber)

if __name__ == '__main__':
    main()  # without arguments given, those will be read from the CLI

Then, we get the following printouts:

$ ./my_cli.py red yellow
Our fruits are: red yellow green

$ ./my_cli.py red yellow --cucumber=purple
Our fruits are: red yellow purple

$ ./my_cli.py red yellow -c nice
Our fruits are: red yellow nice

$ ./my_cli.py red
usage: my_cli.py [-h] [--cucumber | -c  CUCUMBER] apple banana
my_cli.py: error: the following arguments are required: banana

$ ./my_cli.py --help
usage: my_cli.py [-h] [--cucumber | -c  CUCUMBER] apple banana

positional arguments:
  apple
  banana

optional arguments:
  -h, --help            show this help message and exit
  --cucumber | -c  CUCUMBER
                        default: green

Decorate a main class

If main functionality is built into a class, the decorator can be used for its contructor __init__ method, as well as for the class itself, like in the example below.

#!/usr/bin/env python3
from simple_cli_args import cli_args

@cli_args
class Main:
    def __init__(self, apple, banana, cucumber='green'):
        self.fruits = apple, banana, cucumber
    def show(self):
        print("Our fruits are:", *self.fruits)

if __name__ == '__main__':
    main = Main()  # without arguments given, those will be read from the CLI
    main.show()

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

simple_cli_args-1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

simple_cli_args-1.0-py3.8.egg (6.6 kB view details)

Uploaded Egg

simple_cli_args-1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file simple_cli_args-1.0.tar.gz.

File metadata

  • Download URL: simple_cli_args-1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.0

File hashes

Hashes for simple_cli_args-1.0.tar.gz
Algorithm Hash digest
SHA256 e31d434579828551a89d14b31e5861eb0f78b4d9ce0b8b772bd5273917debb6b
MD5 13cd52ceedc920d9dc3391a5e7b8fd48
BLAKE2b-256 52771b8e244839269aa43f1adb970322f4b9ea498cc0ee0295691e771b1f28ff

See more details on using hashes here.

File details

Details for the file simple_cli_args-1.0-py3.8.egg.

File metadata

  • Download URL: simple_cli_args-1.0-py3.8.egg
  • Upload date:
  • Size: 6.6 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.0

File hashes

Hashes for simple_cli_args-1.0-py3.8.egg
Algorithm Hash digest
SHA256 65c6f7e459c60b3f25b56edefd02d21c115ff278ef898b763791db95611e4dc1
MD5 d96f773d04848dce7610748443f6f3f7
BLAKE2b-256 1e79401ae63a416b193526aec2833dfee971cd14dab3e2cb919afd22568ec7fe

See more details on using hashes here.

File details

Details for the file simple_cli_args-1.0-py3-none-any.whl.

File metadata

  • Download URL: simple_cli_args-1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.0

File hashes

Hashes for simple_cli_args-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 119e039bf36ae2b375dfc8f13b57195da757ee5750fbb72c54799bee623290b9
MD5 edb9cdabee7a146b47a795fe19b5c352
BLAKE2b-256 85809259be25906113aef65811756c031470d781bbb4e704329f515f20af5da7

See more details on using hashes here.

Supported by

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