commit 4c89833441d9cdeb4ac5a6c5d82d4d5ef40be1a5
parent 3c386477a1e7219a4989e35b0cd8859b4a01fcb0
Author: Antoine Amarilli <a3nm@a3nm.net>
Date: Wed, 8 Jan 2025 16:23:45 +0100
use argparse to parse CLI arguments reasonably
(thanks, ChatGPT)
Diffstat:
maketex.py | | | 39 | ++++++++++++++------------------------- |
1 file changed, 14 insertions(+), 25 deletions(-)
diff --git a/maketex.py b/maketex.py
@@ -12,34 +12,26 @@
from parserec import parse, getvenue, isurlrel, absurl, getyear, endswithpunct, authorname, stz
import os
import sys
+import argparse
SITEURL="https://a3nm.net"
LOCALURL=SITEURL+"/publications/"
-# add full acronyms, page numbers
-formal_mode = False
-if len(sys.argv) > 5:
- print("too many arguments", file=sys.stderr)
- sys.exit(1)
+parser = argparse.ArgumentParser()
+parser.add_argument('lang', nargs='?', default='en', help="Language (default: 'en').")
+parser.add_argument('fmt', nargs='?', default='tex', help="Format (can be 'tex' for cvitems or 'bib' for bibtex, default: 'tex').")
+parser.add_argument('thepubli', nargs='?', default="all", help="ID of the specific publication to generate (default: 'all'; can also be 'cv').")
+parser.add_argument('--formal', action='store_true', help="Add full venue names and page numbers")
-lang = "en"
-try:
- lang = sys.argv[1]
-except IndexError:
- pass
+# Parse arguments
+args = parser.parse_args()
-fmt = 'tex'
-try:
- fmt = sys.argv[2]
-except IndexError:
- pass
-
-thepubli = None
-try:
- thepubli = sys.argv[3]
-except IndexError:
- pass
+# Assign to global variables to maintain compatibility
+lang = args.lang
+fmt = args.fmt
+thepubli = args.thepubli
+formal_mode = args.formal
# http://stackoverflow.com/a/1432949
abspath = os.path.abspath(__file__)
@@ -208,12 +200,9 @@ fmtdict = {
'bib': publitobib,
}
-if thepubli == 'all_formal':
- formal_mode = True
-
if __name__ == '__main__':
for publi in parse('publis'):
- if thepubli not in ["all", "all_formal"]:
+ if thepubli not in ["all"]:
if thepubli == "cv" or not thepubli:
if publi.get('reviewed', "no") == "no":
# only reviewed publis