Skip to main content

Pythonic commandline applications

Project description

# Wing Commander

Status: [![Travis Status](https://travis-ci.org/jdost/wingcommander.svg?branch=master)](https://travis-ci.org/jdost/wingcommander)

## Making python's `cmd` module better

The `cmd` module for python makes writing readline applications accessible, this is
just an improved wrapper on this. It allows for you to create instances easily
using decorators and comes with a varied toolbelt for handling various use cases of
the system.

## Easy creation

```python
from wingcommander import WingCommander, run


class TestShell(WingCommander):
pass


@TestShell.command
def hello(cmdr, *args):
''' Say hello:
echoes 'hello'
'''
print("hello")


if __name__ == '__main__':
commander = TestShell(name="sample")
run(commander)
```

The `TestShell.command` decorator converts the `hello` function into a command for
the readline instance. The help text for the command gets pulled from the docstring
(can be overriden).

## Tab completion

You can include a `completions` argument in the decorator to enable tab completion
for the function as well:

```python
@TestShell.command(completions=["world", "jdost"])
```

This takes a function, `dict`, or `list`. If it is a list, it will complete
anything from that list for every argument, if it is a dictionary, it will complete
a key for the first argument and then one of the values for the second (you can nest
dictionaries too). For a function, it works the same as the [`completedefault`][1]
function.

[1]: https://docs.python.org/2/library/cmd.html#cmd.Cmd.completedefault

## Subshells

WingCommander supports subshells out of the box. This means that you can import
and start another WingCommander command shell inside of an existing one and it will
both denote the state and transition smoothly.

## Toolbelt

This also comes with a set of helper functions to ease various output scenarios.

### `watch`

```python
def cat():
f = open("editthis.txt", "r")
return [l.strip() for l in f.readlines()]

wingcommander.watch(cat)
```

The first is the `watch` function. This works in the same way as the unix `watch`
command where it will print the output of the supplied function on a determined
frequency. The output can be multiline and the area covered will grow as the output
gets larger. This uses ANSI control characters (has only been tested using `urxvt`
for now).

### `tablize`

```python
def list_users():
return [["jdost", "github", "python"],
["tester", "twitter", "tweets"]]

print(tablize(list_users()))
```

The `tablize` function takes a list of lists (or dictionaries) and creates an
aligned output in a table form. The above command would output:
```
jdost | github | python
tester | twitter | tweets
```

The function takes various optional arguments such as `max_length`, `keys`,
`dividers`, `labels`, and `alignment`.

* `max_length` is just a number that limits the width any column can be (if it is a
list, each number will be applied to the corresponding column)
* `keys` is for use with a list of dictionaries, rather than printing all the keys,
just the keys in the list will be printed (in the order they are listed)
* `dividers` is a list or dictionary giving the characters to use as dividers, the
order is [`VERTICAL`, `HORIZONTAL`, `CROSS`], any omitted will use the defaults,
* `VERTICAL` are the pipes that separate the columns, default: `|`
* `HORIZONTAL` are the spaces that divide the rows, if empty, no divider is used
(default)
* `CROSS` is the character used (if both `VERTICAL` and `HORIZONTAL` are set) to
mark the intersection of these markers
* `labels` is a list of strings that will label the columns
* `alignment` is a list of characters that denote how the columns will be aligned,
options are `c` for center, `r` for right, and `l` for left, you can also use
`tablize.LEFT`, `tablize.CENTER`, and `tablize.RIGHT` (this is preferable)

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

wingcommander-0.1.5.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

wingcommander-0.1.5-py2-none-any.whl (10.0 kB view details)

Uploaded Python 2

File details

Details for the file wingcommander-0.1.5.tar.gz.

File metadata

  • Download URL: wingcommander-0.1.5.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for wingcommander-0.1.5.tar.gz
Algorithm Hash digest
SHA256 53f3be8d6985f3d942c2ff08384b5a8502ef5bb7d2dee68df655a64a3c06c9f5
MD5 2cdd871cd45d0f7131d98c93de211de3
BLAKE2b-256 ddfa738bf3ee8469e5ee8a4bf78acf54031df31ee095124dffc1423019f096df

See more details on using hashes here.

File details

Details for the file wingcommander-0.1.5-py2-none-any.whl.

File metadata

File hashes

Hashes for wingcommander-0.1.5-py2-none-any.whl
Algorithm Hash digest
SHA256 73f9abfc1fca55c70e1145d06e49eab0e67fb502f84716caa120ee3bfdc4dbc8
MD5 c0bc3379894d7803e96ba1dbce5b389f
BLAKE2b-256 c3bfc924efb40075a3ef85a00190f4bed90a1f5e4fbbb98b923412a0189e12ad

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