Not Logged In

argh 0.26.1

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);

Term-Friendly:

Command output is processed with respect to stream encoding;

Unobtrusive:

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

DRY:

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);

add an alias root command help for the --help argument.

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

Using pip:

$ pip install argh

Arch Linux (AUR):

$ yaourt python-argh

Examples

A very simple application with one command:

importarghdefmain():return'Hello world'argh.dispatch_command(main)

Run it:

$ ./app.py
Hello world

A potentially modular application with multiple commands:

importargh# declaring:defecho(text):"Returns given word as is."returntextdefgreet(name,greeting='Hello'):"Greets the user with given name. The greeting is customizable."returngreeting+', '+name# assembling:parser=argh.ArghParser()parser.add_commands([echo,greet])# dispatching:if__name__=='__main__':parser.dispatch()

# 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:defcmd(foo,bar=1,*args,**kwargs):yieldfooyieldbaryield', '.join(args)yieldkwargs['baz']yieldkwargs['quux']

Author

Support

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

Otherwise you can “flattr” me:

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/>.