All Projects → anntzer → Defopt

anntzer / Defopt

Licence: mit
Effortless argument parser

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Defopt

Taskwarrior
Taskwarrior - Command line Task Management
Stars: ✭ 2,239 (+1392.67%)
Mutual labels:  cli
Wikibase Cli
read and edit a Wikibase instance from the command line
Stars: ✭ 148 (-1.33%)
Mutual labels:  cli
Athenacli
AthenaCLI is a CLI tool for AWS Athena service that can do auto-completion and syntax highlighting.
Stars: ✭ 151 (+0.67%)
Mutual labels:  cli
Mojo Webqq
【重要通知:WebQQ将在2019年1月1日停止服务,此项目目前已停止维护,感谢大家四年来的一路陪伴】使用Perl语言(不会没关系)编写的smartqq/webqq客户端框架(非GUI),可通过插件提供基于HTTP协议的api接口供其他语言或系统调用
Stars: ✭ 1,755 (+1070%)
Mutual labels:  cli
Morphlingjs
A CLI to mock with meaningful data a REST API from a Swagger file
Stars: ✭ 148 (-1.33%)
Mutual labels:  cli
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (+0%)
Mutual labels:  cli
Protolint
A pluggable linter and fixer to enforce Protocol Buffer style and conventions.
Stars: ✭ 142 (-5.33%)
Mutual labels:  cli
Pomd
🍅 A good old cli based Pomodoro timer with native notifications
Stars: ✭ 151 (+0.67%)
Mutual labels:  cli
Kcli
A kafka command line browser
Stars: ✭ 148 (-1.33%)
Mutual labels:  cli
Assert cli
See instead assert_cmd
Stars: ✭ 150 (+0%)
Mutual labels:  cli
Hub Tool
🧪 Docker Hub experimental CLI tool
Stars: ✭ 147 (-2%)
Mutual labels:  cli
Python Sploitkit
Devkit for building Metasploit-like consoles
Stars: ✭ 148 (-1.33%)
Mutual labels:  cli
Envy
Envy automatically exposes environment variables for all of your Go flags
Stars: ✭ 150 (+0%)
Mutual labels:  cli
Musicnow
CLI tool to download songs with metadata.
Stars: ✭ 146 (-2.67%)
Mutual labels:  cli
Multiprocess
🚀Easy to make the common PHP/Python/js...script change daemon and multi-process execution
Stars: ✭ 151 (+0.67%)
Mutual labels:  cli
Sfz
A simple static file serving command-line tool written in Rust.
Stars: ✭ 145 (-3.33%)
Mutual labels:  cli
Ipgetter
Utility to fetch your external IP address
Stars: ✭ 148 (-1.33%)
Mutual labels:  cli
Gitin
commit/branch/workdir explorer for git
Stars: ✭ 1,815 (+1110%)
Mutual labels:  cli
Ffpb
A progress bar for ffmpeg. Yay !
Stars: ✭ 149 (-0.67%)
Mutual labels:  cli
Xcassetpacker
A command line tool for converting a folder of images into an .xcasset package for Xcode
Stars: ✭ 150 (+0%)
Mutual labels:  cli

defopt

| |GitHub| |PyPI| |conda-forge| | |Read the Docs| |Build|

.. |GitHub| image:: https://img.shields.io/badge/github-anntzer%2Fdefopt-brightgreen :target: GitHub repository_ .. |PyPI| image:: https://img.shields.io/pypi/v/defopt.svg?color=brightgreen :target: https://pypi.python.org/pypi/defopt .. |conda-forge| image:: https://img.shields.io/conda/v/conda-forge/defopt.svg?label=conda-forge&color=brightgreen :target: https://anaconda.org/conda-forge/defopt .. |Read the Docs| image:: https://img.shields.io/readthedocs/defopt :target: Read the Docs_ .. |Build| image:: https://img.shields.io/github/workflow/status/anntzer/defopt/build :target: https://github.com/anntzer/defopt/actions

defopt is a lightweight, no-effort argument parser.

defopt will:

  • Allow functions to be run from code and the command line without modification.
  • Reward you for documenting your functions.
  • Save you from writing, testing and maintaining argument parsing code.

defopt will not:

  • Modify your functions in any way.
  • Allow you to build highly complex or customized command line tools.

If you want total control over how your command line looks or behaves, try docopt_, click_ or argh_. If you just want to write Python code and leave the command line interface up to someone else, defopt is for you.

Usage

Once you have written and documented_ your function, simply pass it to defopt.run() and you're done.

.. code-block:: python

import defopt

# Use type hints:
def main(greeting: str, *, count: int = 1):
    """
    Display a friendly greeting.

    :param greeting: Greeting to display
    :param count: Number of times to display the greeting
    """
    for _ in range(count):
        print(greeting)

# ... or document parameter types in the docstring:
def main(greeting, *, count=1):
    """
    Display a friendly greeting.

    :param str greeting: Greeting to display
    :param int count: Number of times to display the greeting
    """
    for _ in range(count):
        print(greeting)

if __name__ == '__main__':
    defopt.run(main)

Descriptions of the parameters and the function itself are used to build an informative help message.

::

$ python test.py -h
usage: test.py [-h] [-c COUNT] greeting

Display a friendly greeting.

positional arguments:
  greeting              Greeting to display

optional arguments:
  -h, --help            show this help message and exit
  -c COUNT, --count COUNT
                        Number of times to display the greeting
                        (default: 1)

Your function can now be called identically from Python and the command line.

::

>>> from test import main
>>> main('hello!', count=2)
hello!
hello!

::

$ python test.py hello! --count 2
hello!
hello!

Philosopy

defopt was developed with the following guiding principles in mind:

#. The interface can be fully understood in seconds. If it took any longer, your time would be better spent learning a more flexible tool.

#. Anything you learn applies to the existing ecosystem. The exact same docstrings used by defopt are also used by Sphinx's autodoc_ extension to generate documentation, and by your IDE to do type checking. Chances are you already know everything you need to know to use defopt.

#. Everything is handled for you. If you're using defopt, it's because you don't want to write any argument parsing code at all. You can trust it to build a logically consistent command line interface to your functions with no configuration required.

#. Your Python functions are never modified. Type conversions are only ever applied to data originating from the command line. When used in code, duck-typing still works exactly as you expect with no surprises.

Development

For source code, examples, questions, feature requests and bug reports, visit the GitHub repository_.

Documentation

Documentation is hosted on Read the Docs_.

.. _GitHub repository: https://github.com/anntzer/defopt .. _Read the Docs: https://defopt.readthedocs.io/en/latest/ .. _autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html .. _docopt: http://docopt.org/ .. _click: http://click.palletsprojects.com/ .. _argh: https://argh.readthedocs.io/en/latest/ .. _documented: https://defopt.readthedocs.io/en/latest/features.html#docstring-styles

.. This document is included in docs/index.rst; table of contents appears here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].