changeset 4176:121925996ffb

cli (blog): new `--alt-link` option to specify alternate links
author Goffi <goffi@goffi.org>
date Tue, 05 Dec 2023 13:14:33 +0100
parents 30f7513e5590
children 0f1a4ffcd419
files libervia/cli/cmd_blog.py
diffstat 1 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/cli/cmd_blog.py	Tue Dec 05 13:14:03 2023 +0100
+++ b/libervia/cli/cmd_blog.py	Tue Dec 05 13:14:33 2023 +0100
@@ -18,6 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
+import argparse
 import asyncio
 from asyncio.subprocess import DEVNULL
 from configparser import NoOptionError, NoSectionError
@@ -110,6 +111,35 @@
     return await host.bridge.param_get_a("Syntax", "Composition", "value", host.profile)
 
 
+class AltLinkAction(argparse.Action):
+    def __init__(self, option_strings, dest, nargs="+", **kwargs):
+        if nargs != "+":
+            raise ValueError('nargs must be "+"')
+
+        super().__init__(option_strings, dest, nargs=nargs, **kwargs)
+
+    def __call__(self, parser, namespace, values, option_string=None):
+        assert values
+        url = values[0]
+        if len(values) == 1:
+            media_type = None
+        elif len(values) == 2:
+            media_type = values[1]
+        else:
+            parser.error(
+                f"invalid number of argument for {', '.join(self.option_strings)}, it "
+                "must have at most 2 arguments."
+            )
+        alt_link = {'url': url}
+        if media_type is not None:
+            alt_link['media_type'] = media_type
+        alt_links = getattr(namespace, self.dest)
+        if alt_links is None:
+            alt_links = []
+            setattr(namespace, self.dest, alt_links)
+        alt_links.append(alt_link)
+
+
 class BlogPublishCommon:
     """handle common option for publising commands (Set and Edit)"""
 
@@ -164,6 +194,17 @@
             )
         )
 
+        self.parser.add_argument(
+            '--alt-link',
+            action=AltLinkAction,
+            dest="alt_links",
+            metavar=('URL', 'MEDIA_TYPE'),
+            help=(
+                "add an alternative link, you can use {service}, {node} and {item} "
+                "template values in URL"
+            )
+        )
+
         comments_group = self.parser.add_mutually_exclusive_group()
         comments_group.add_argument(
             "-C",
@@ -284,6 +325,8 @@
             mb_data["language"] = self.args.language
         if self.args.no_id_suffix:
             mb_data["user_friendly_id_suffix"] = False
+        if self.args.alt_links:
+            mb_data.setdefault("extra", {})["alt_links"] = self.args.alt_links
         if self.args.encrypt:
             mb_data["encrypted"] = True
         if self.args.sign: