Skip to main content

An unobtrusive argparse wrapper with natural syntax

Project description

https://github.com/neithere/argh/actions/workflows/lint-and-test.yml/badge.svg https://img.shields.io/pypi/format/argh.svg https://img.shields.io/pypi/status/argh.svg https://img.shields.io/pypi/v/argh.svg https://img.shields.io/pypi/pyversions/argh.svg https://img.shields.io/pypi/dd/argh.svg https://readthedocs.org/projects/argh/badge/?version=stable https://readthedocs.org/projects/argh/badge/?version=latest

Building a command-line interface? Found yourself uttering “argh!” while struggling with the API of argparse? Don’t like the complexity but need the power?

Everything should be made as simple as possible, but no simpler.

—Albert Einstein (probably)

Argh is a smart wrapper for argparse. Argparse is a very powerful tool; Argh just makes it easy to use.

In a nutshell

Argh-powered applications are simple but flexible:

Modular:

Declaration of commands can be decoupled from assembling and dispatching;

Pythonic:

Commands are declared naturally, no complex API calls in most cases;

Reusable:

Commands are plain functions, can be used directly outside of CLI context;

Layered:

The complexity of code raises with requirements;

Transparent:

The full power of argparse is available whenever needed;

Namespaced:

Nested commands are a piece of cake, no messing with subparsers (though they are of course used under the hood);

Unobtrusive:

Argh can dispatch a subset of pure-argparse code, and pure-argparse code can update and dispatch a parser assembled with Argh;

DRY:

Don’t Repeat Yourself. The amount of boilerplate code is minimal. Among other things, Argh will:

  • infer command name from function name;

  • infer arguments from function signature;

  • infer argument type from the default value;

  • infer argument action from the default value (for booleans);

NIH free:

Argh supports completion, progress bars and everything else by being friendly to excellent 3rd-party libraries. No need to reinvent the wheel.

Sounds good? Check the tutorial!

Relation to argparse

Argh is fully compatible with argparse. You can mix Argh-agnostic and Argh-aware code. Just keep in mind that the dispatcher does some extra work that a custom dispatcher may not do.

Installation

$ pip install argh

Examples

A very simple application with one command:

import argh

def main() -> str:
    return "Hello world"

argh.dispatch_command(main)

Run it:

$ ./app.py
Hello world

An app with multiple commands:

import argh

from my_commands import hello, echo

argh.dispatch_commands([hello, echo])

Run it:

$ ./app.py echo Hey
Hey

A potentially modular application with more control over the process:

import argh

# declaring:

def echo(text):
    "Returns given word as is."
    return text

def greet(name: str, *, greeting: str = "Hello") -> str:
    "Greets the user with given name. The greeting is customizable."
    return f"{greeting}, {name}!"

# assembling:

parser = argh.ArghParser()
parser.add_commands([echo, greet])

# dispatching:

if __name__ == "__main__":
    parser.dispatch()
$ ./app.py greet Andy
Hello, Andy

$ ./app.py greet Andy -g Arrrgh
Arrrgh, Andy

Here’s the auto-generated help for this application (note how the docstrings are reused):

$ ./app.py --help

usage: app.py {echo,greet} ...

positional arguments:
    echo        Returns given word as is.
    greet       Greets the user with given name. The greeting is customizable.

…and for a specific command (an ordinary function signature is converted to CLI arguments):

$ ./app.py --help greet

usage: app.py greet [-g GREETING] name

Greets the user with given name. The greeting is customizable.

positional arguments:
  name

optional arguments:
  -g GREETING, --greeting GREETING   'Hello'

(The help messages have been simplified a bit for brevity.)

Argh easily maps plain Python functions to CLI. Sometimes this is not enough; in these cases the powerful API of argparse is also available:

@arg("text", default="hello world", nargs="+", help="The message")
def echo(text: str) -> None:
    print text

The approaches can be safely combined even up to this level:

# adding help to `foo` which is in the function signature:
@arg("foo", help="blah")
# these are not in the signature so they go to **kwargs:
@arg("baz")
@arg("-q", "--quux")
# the function itself:
def cmd(foo: str, bar: int = 1, *args, **kwargs) -> Iterator[str]:
    yield foo
    yield bar
    yield ", ".join(args)
    yield kwargs["baz"]
    yield kwargs["quux"]

Author

Developed by Andrey Mikhaylenko since 2010.

See file AUTHORS.rst for a list of contributors to this library.

Support

The fastest way to improve this project is to submit tested and documented patches or detailed bug reports.

You can also donate via Liberapay. This may speed up development or simply make the original author happy :)

Licensing

Argh is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Argh is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with Argh. If not, see <http://gnu.org/licenses/>.

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

argh-0.30.2.tar.gz (61.5 kB view details)

Uploaded Source

Built Distribution

argh-0.30.2-py3-none-any.whl (44.4 kB view details)

Uploaded Python 3

File details

Details for the file argh-0.30.2.tar.gz.

File metadata

  • Download URL: argh-0.30.2.tar.gz
  • Upload date:
  • Size: 61.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for argh-0.30.2.tar.gz
Algorithm Hash digest
SHA256 25b2bca3a6770ee41d76c4a32825b737412adddc7f850a6c77bc8a4f37122dcb
MD5 0fb5c46a8d4839514bd69807201dd2e0
BLAKE2b-256 21c205b17b97f7900b59d969d4c2555d6b406bd93bdaed17d12bb2e1f0a94e82

See more details on using hashes here.

File details

Details for the file argh-0.30.2-py3-none-any.whl.

File metadata

  • Download URL: argh-0.30.2-py3-none-any.whl
  • Upload date:
  • Size: 44.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for argh-0.30.2-py3-none-any.whl
Algorithm Hash digest
SHA256 08f8fced786cc89d59707f917f28db3b3582ffc35331b32053c0ae3f754e5c99
MD5 250e8f4fc17110e36e6a0a3f1cae379f
BLAKE2b-256 a334357450c62604ecf36e767d28d8d99db83784d57dcf4a984fd7c80ed3ce25

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