Skip to main content

Choose which functions to execute by simply creating clear cmdline menu and cmdline args parser in one step

Project description

ArgChooser

ArgChooser is Python module which allows for fast and easy creation of function chooser.

  • Gives end-user an option to choose which functions to execute by cmdline args or cmdline menu
  • Just add flag names and functions
  • Execute and watch magic!

Table of Contents

  1. Documentation
    1. Installation
    2. Quick Start
    3. Examples
  2. License

Documentation

Installation

Simply use pip to install the ArgChooser package

pip install argchoose

Quick Start

Using ArgChooser is as simple as possible. Simple three steps:

  • Create object of ArgChooser
  • Bind methods with flags using add_argument
  • Execute ArgChooser!
from argchoose import ArgChooser


def foo():
    print('bar')


ac = ArgChooser()
ac.add_argument('-f', '--foo', method=foo)
ac.execute()

Now You are able to run your program from cmdline like that to execute method:

python example.py -f

Or without flags to see this:

Quickstart menu example

Examples

  1. Adding arguments
  2. Changing default ArgChooser parameters

Adding arguments

add_argument - allows to bind flag (or flags) with chosen method. It is possible to also specify category and help prompt.

Takes *flags: str, method: Callable [, help: str, category: str]

Returns nothing.

Example is based on This.

Categories allows to make menu clearer.

Default menu example

As You can see, help also makes it more descriptive, but also could be printed using -h flag like cmdline tools usually do!

python default_usage.py -h

Default help

To execute chosen methods, end-user has to choose, if they want to use it straight from cmdline or menu:

Default menu results

Default cmdline

Changing default ArgChooser parameters

ArgChooser could be initialized without any parameters, but gives possibility to change a few little things as shown Here.

Takes [category_style: str, default_category_name: str, style: Dict[Token, str], validation_func: Callable(List) -> bool, validation_error: str].

category_style is string on which is used format method with category name.
{} must be included in string.
Default is == {} ==.

default_category_name is string which will be used as category name for every uncategorized methods. Default is Other.

style is dictionary made from Tokens and strings which represents hex colors with textformatting keywords (like bold).

validation_func is function used for validating answers at the end of parsing (for example for checking if amount of answers is bigger than two).
This function must take one argument which is List and must return bool value.
Default functions checks if any answer is chosen.

validation_error is string which will be printed if validation_func returned False.
Default is You must choose at least one module..

Example

When validation_func return False, validation_error is printed like that:

Error

This is going to be changed to this, after releasing new version of PyInquirer with fixed validation parameter:

Correct error

License

Copyright (c) 2020 Paweł Kusiński
Distributed under the MIT License. See LICENSE for more information.

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

argchoose-0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file argchoose-0.1-py3-none-any.whl.

File metadata

  • Download URL: argchoose-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.1.2 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for argchoose-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ee55656542d54bd5deb57f93712efd2d7dee6456db6ce8871b0bc3ccbfa1e9ab
MD5 dc242514d8bde09a9e48cc8c7fed0677
BLAKE2b-256 167e2c139990ab216d69e4245e9c4b8d1cc22b103e2165ca36a5cea01ecbd57b

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