Skip to content

Commit 535d1ff

Browse files
authored
Merge pull request #35 from mittel-labs/feat/enhancing-commands
Enhancing commands
2 parents f2469a7 + 7468bfe commit 535d1ff

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

src/bibleit/command/__init__.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ def eval_module(name):
3131
return eval_module(_default_module)
3232

3333

34-
def eval(ctx, *line, module=None):
34+
def eval(ctx, *line, module=None): # noqa: C901
3535
"""Evaluate a function with arbitrary arguments."""
3636
if module is None:
3737
module = _default_module
3838
try:
3939
name, *args = line
4040

41-
if name.startswith("!"):
42-
args.insert(0, name[1:])
43-
name = "set"
41+
if any(name.startswith(alias) for alias in core._ALIASES):
42+
alias = name.strip()[0]
43+
name = "".join(filter(None, map(str.strip, name.split(alias))))
44+
if target := core._ALIASES.get(alias):
45+
args.insert(0, name)
46+
name = target
4447

4548
ctx.module = eval_module(module)
4649
ctx.methods = eval_methods(ctx.module)

src/bibleit/command/core.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@
99

1010
from operator import attrgetter as _attrgetter
1111

12+
_ALIASES = {
13+
"!": "set",
14+
"@": "ref",
15+
"?": "help",
16+
"&": "search",
17+
"%": "count",
18+
"-": "note",
19+
"\\": "notes",
20+
}
21+
22+
23+
def _format_alias(name):
24+
if alias := next((alias for alias, cmd in _ALIASES.items() if cmd == name), None):
25+
return f"(alias: '{alias}')"
26+
return ""
27+
1228

1329
def _format_lines(lines):
1430
linesep = "\n" * (_config.linesep + 1)
@@ -29,8 +45,13 @@ def _ref_parse(ctx, bible_fn, target, term):
2945
return result if result else f"{term} '{' '.join(target)}' not found"
3046

3147

48+
def _doc(hdoc, name):
49+
if hdoc and name:
50+
return hdoc.replace("@alias", _format_alias(name))
51+
52+
3253
def help(ctx, *args):
33-
"""Prints this help."""
54+
"""Prints this help. @alias"""
3455
if args:
3556
match args:
3657
case ["help", *sub_command]:
@@ -40,17 +61,18 @@ def help(ctx, *args):
4061
sys.modules[__name__], name
4162
), f"command '{name}' not found"
4263
if target := getattr(sys.modules[__name__], name).__doc__:
43-
print(target)
64+
print(_doc(target, name))
4465
case [name, sub_command]:
4566
module = _command.eval_module(name)
46-
print(getattr(module, sub_command).__doc__)
67+
if target := getattr(module, sub_command).__doc__:
68+
print(_doc(target, name))
4769
case _:
4870
print(
4971
"Error: You must use help for command and one sub-command only.\n\nhelp <command> [<sub-command>]"
5072
)
5173
else:
5274
methods = sorted(
53-
f"{method.__name__:<20} {method.__doc__.splitlines()[0]}"
75+
f"{method.__name__:<20} {_doc(method.__doc__.splitlines()[0], method.__name__)}"
5476
for method in [getattr(ctx.module, name) for name in ctx.methods]
5577
)
5678

@@ -59,7 +81,7 @@ def help(ctx, *args):
5981

6082

6183
def set(ctx, *args):
62-
"""Configure a sub-command with a new value.
84+
"""Configure a sub-command with a new value. @alias
6385
6486
set <config> <value>
6587
@@ -69,7 +91,7 @@ def set(ctx, *args):
6991

7092

7193
def ref(ctx, *args):
72-
"""Search a reference(s) by chapters and verses.
94+
"""Search a reference(s) by chapters and verses. @alias
7395
7496
ref <book> [<chapter>[:<verse[-verse]>]]
7597
@@ -85,7 +107,7 @@ def ref(ctx, *args):
85107

86108

87109
def search(ctx, *args):
88-
"""Search one or more words.
110+
"""Search one or more words. @alias
89111
90112
search <word [word2 [...]]>
91113
@@ -101,7 +123,7 @@ def search(ctx, *args):
101123

102124

103125
def count(ctx, *args):
104-
"""Count how much words.
126+
"""Count how much words. @alias
105127
106128
count <word [word2 [...]]>
107129
@@ -164,16 +186,17 @@ def blb(ctx, *args):
164186

165187

166188
def notes(ctx, *args):
167-
"""List all notes."""
189+
"""List all notes. @alias"""
190+
assert not args, "you should use only notes"
168191
return _format_lines(ctx.notes) if ctx.notes else None
169192

170193

171194
def note(ctx, *args):
172-
"""Adds a new note.
195+
"""Adds a new note. @alias
173196
174197
notes <string>"""
175198
target = " ".join(args)
176199

177-
assert target, "you should use notes <string>"
200+
assert target, "you should use note <string>"
178201
ctx.add_note(target)
179202
return None

src/bibleit/command/set.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def bible(ctx, *args):
3939
Examples:
4040
set bible kjv
4141
set bible acf, nvi"""
42+
assert args, "you should use bible <translation[, translation[, ...]]>"
4243
translations = [value for arg in args for value in arg.split(",") if value]
4344
ctx.bible = sorted(
4445
{_Bible(translation.lower()) for translation in translations},

0 commit comments

Comments
 (0)