Werkzeug-style command parsing.
Project description
# commandlet
Werkzeug-style commands.
## Usage
```
from commandlet.parser import Parser
p = Parser()
@p.command('test', 'test <int:times> <str:string>')
def do_test(times, string):
"""Test a certain number of times."""
for n in range(times):
print('[%d]: %s' % (n, string))
p.handle_command('test 4 Hello world.')
```
As you can see, you can decorate functions with the Parser.command decorator to make them callable. They should all be given a name (used for pretty-printing mainly), and an argument string, which looks very similar to those seen in command line programs... And [Werkzeug](https://palletsprojects.com/p/werkzeug/).
You can also add extra filters:
```
@p.filter('reverse')
def do_reverse(text):
return (text, ''.join(reversed(text)))
@p.command('reverse', 'reverse <reverse:string>')
def reverse_command(string):
original, new = string
print('Reversing %r gives %r.' % (original, new))
```
You can see the full list of filters by examining the Parser.filters dictionary. By default, str, int, and float are supported.
Werkzeug-style commands.
## Usage
```
from commandlet.parser import Parser
p = Parser()
@p.command('test', 'test <int:times> <str:string>')
def do_test(times, string):
"""Test a certain number of times."""
for n in range(times):
print('[%d]: %s' % (n, string))
p.handle_command('test 4 Hello world.')
```
As you can see, you can decorate functions with the Parser.command decorator to make them callable. They should all be given a name (used for pretty-printing mainly), and an argument string, which looks very similar to those seen in command line programs... And [Werkzeug](https://palletsprojects.com/p/werkzeug/).
You can also add extra filters:
```
@p.filter('reverse')
def do_reverse(text):
return (text, ''.join(reversed(text)))
@p.command('reverse', 'reverse <reverse:string>')
def reverse_command(string):
original, new = string
print('Reversing %r gives %r.' % (original, new))
```
You can see the full list of filters by examining the Parser.filters dictionary. By default, str, int, and float are supported.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
commandlet-0.4.4.tar.gz
(11.6 kB
view hashes)
Built Distribution
commandlet-0.4.4-py3-none-any.whl
(11.5 kB
view hashes)
Close
Hashes for commandlet-0.4.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 478a68cd85437727ff2598ded3e49a4b49b2aaab8515a5d652748c2061f663aa |
|
MD5 | 569ff0418ffb736f7d97c2886920ce40 |
|
BLAKE2b-256 | e3e55d83080adf8ff7a45403dcfa84f1d18c81a697432704643befb0cf178bcf |