aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2020-09-16 12:43:40 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2020-09-16 12:43:40 +0200
commit6cce5380ef1585f72dc4083297932489f793ca0b (patch)
treeced8c374c1a3a0c162c8898c08f71657f15d5222
parentc7b332764115de5c41a518c64ce902adc3b9b924 (diff)
Make the main command boilerplate available to library users.HEADmaster
-rw-r--r--lesana/command.py58
-rwxr-xr-xscripts/lesana17
2 files changed, 57 insertions, 18 deletions
diff --git a/lesana/command.py b/lesana/command.py
index 407544a..85ed73b 100644
--- a/lesana/command.py
+++ b/lesana/command.py
@@ -1,3 +1,4 @@
+import argparse
import logging
import os
import subprocess
@@ -58,9 +59,35 @@ def edit_file_in_external_editor(filepath):
return True
-class Command:
- help = ''
+def _get_first_docstring_line(obj):
+ try:
+ return obj.__doc__.split('\n')[1].strip()
+ except (AttributeError, IndexError):
+ return None
+
+
+class MainCommand:
+ commands = ()
+
+ def _main(self, args):
+ self.parser.print_help()
+
+ def main(self):
+ desc = _get_first_docstring_line(self)
+ self.parser = argparse.ArgumentParser(description=desc)
+ self.parser.set_defaults(func=self._main)
+ self.subparsers = self.parser.add_subparsers()
+ for name, sub in self.commands:
+ sub_help = _get_first_docstring_line(sub)
+ s_parser = self.subparsers.add_parser(name, help=sub_help)
+ for arg in sub.arguments:
+ s_parser.add_argument(*arg[0], **arg[1])
+ s_parser.set_defaults(func=sub._main)
+ self.args = self.parser.parse_args()
+ self.args.func(self.args)
+
+class Command:
def __init__(self, collection_class=Collection, entry_class=Entry):
self.collection_class = collection_class
self.entry_class = entry_class
@@ -71,6 +98,9 @@ class Command:
class New(Command):
+ """
+ Create a new entry
+ """
arguments = [
(
['--collection', '-c'],
@@ -100,6 +130,9 @@ class New(Command):
class Edit(Command):
+ """
+ Edit a lesana entry
+ """
arguments = [
(
['--collection', '-c'],
@@ -136,6 +169,9 @@ class Edit(Command):
class Show(Command):
+ """
+ Show a lesana entry
+ """
arguments = [
(
['--collection', '-c'],
@@ -169,6 +205,9 @@ class Show(Command):
class Index(Command):
+ """
+ Index entries in a lesana collection
+ """
arguments = [
(
['--collection', '-c'],
@@ -195,6 +234,9 @@ class Index(Command):
class Search(Command):
+ """
+ Search for entries
+ """
arguments = [
(
['--collection', '-c'],
@@ -252,6 +294,9 @@ class Search(Command):
class Export(Command):
+ """
+ Export entries to a different collection
+ """
arguments = [
(
['--collection', '-c'],
@@ -292,6 +337,9 @@ class Export(Command):
class Init(Command):
+ """
+ Initialize a lesana collection
+ """
arguments = [
(
['--collection', '-c'],
@@ -316,6 +364,9 @@ class Init(Command):
class Remove(Command):
+ """
+ Remove an entry from a collection
+ """
arguments = [
(
['--collection', '-c'],
@@ -330,6 +381,9 @@ class Remove(Command):
class Update(Command):
+ """
+ Update a field in multiple entries
+ """
arguments = [
(
['--collection', '-c'],
diff --git a/scripts/lesana b/scripts/lesana
index c8e6b7f..8e34179 100755
--- a/scripts/lesana
+++ b/scripts/lesana
@@ -8,7 +8,7 @@ import argparse
import lesana.command
-class Lesana:
+class Lesana(lesana.command.MainCommand):
"""
Manage collections
"""
@@ -25,21 +25,6 @@ class Lesana:
("rm", lesana.command.Remove()),
)
- def _main(self, args):
- self.parser.print_help()
-
- def main(self):
- self.parser = argparse.ArgumentParser()
- self.parser.set_defaults(func=self._main)
- self.subparsers = self.parser.add_subparsers()
- for name, sub in self.commands:
- s_parser = self.subparsers.add_parser(name, help=sub.help)
- for arg in sub.arguments:
- s_parser.add_argument(*arg[0], **arg[1])
- s_parser.set_defaults(func=sub._main)
- self.args = self.parser.parse_args()
- self.args.func(self.args)
-
if __name__ == "__main__":
Lesana().main()