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-0.20.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

simple_cli_args-0.20-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: simple_cli_args-0.20.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for simple_cli_args-0.20.tar.gz
Algorithm Hash digest
SHA256 c8aa833b7bf31b9ceeda723dedfeea1403648866a57141266f8ca279fba3f49d
MD5 fd38fc3f70ce098ab448572194979973
BLAKE2b-256 4beab2fde579a0899c108ad11c3ffc7348104657ad8f89ba209c42e09245842c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: simple_cli_args-0.20-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0

File hashes

Hashes for simple_cli_args-0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 6fdd107337bed5ea313c1001df3225edf54692de19e40d70206532cfdd33b436
MD5 3407ccb36fca2ba671ff60cac20e9bdf
BLAKE2b-256 d4e30a1ac16aaa3134d001f9799bf58db6b4fd73b68abe0844e7add2ca54ae0c

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